1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;
public class TestSelection {
static Collection selection(Collection source, CritereSelection critere){
Collection result = new Vector();
Iterator iter = source.iterator();
while(iter.hasNext()){
Object aux = iter.next();
if(critere.ok(aux))
result.add(aux);
}
return result;
}
}
interface CritereSelection{
boolean ok (Object x);
}
static class EtreMultipleDe3 implements CritereSelection{
public boolean ok(Object x){
return((Integer)x).IntValue()/3 == 0;
}
}
public static void main(String[] args) {
LinkedList l = new LinkedList();
for(int i=0;i<100;i++)
l.add(i);
System.out.println(l);
Collection l2 = selection(l,new EtreMultipleDe3());
System.out.println(l2);
}
} |
Partager