我想知道下面的代碼中用於實現類「PredTest」中的接口方法的是什麼類型的概念。需要在類中實現接口方法的說明
static Predicate pred = new Predicate() {
public boolean predicate(Object o) {
return o.toString().startsWith("Hi");
}
};
全碼
import java.util.*;
public class PredTest {
static Predicate pred = new Predicate() {
public boolean predicate(Object o) {
return o.toString().startsWith("Hi");
}
};
public static void main(String args[]) {
String [] names = {"java", "ProdTest", "One", "Two", "Hi", "Three", "Four", "High", "Six", "Seven", "Higher"};
List<String> list = Arrays.asList(names);
Iterator<String> i1 = list.iterator();
Iterator<String> i = new PredicateIterator(i1, pred);
while (i.hasNext()) {
System.out.println(i.next());
}
}
}
class PredicateIterator implements Iterator<String> {
Iterator<String> iter;
Predicate pred;
String next;
boolean doneNext = false;
public PredicateIterator(Iterator<String> iter, Predicate pred) {
this.iter = iter;
this.pred = pred;
}
public void remove() {
throw new UnsupportedOperationException();
}
public boolean hasNext() {
doneNext = true;
boolean hasNext;
while (hasNext = iter.hasNext()) {
next = iter.next();
if (pred.predicate(next)) {
break;
}
}
return hasNext;
}
public String next() {
if (!doneNext) {
boolean has = hasNext();
if (!has) {
throw new NoSuchElementException();
}
}
doneNext = false;
return next;
}
}
interface Predicate {
boolean predicate(Object element);
}