Iterator<Integer> itr = list.iterator();
while(itr.hasNext()) {
int i = itr.next();
if (i > 5) {
itr.remove();
}
}
interface Predicate<T>{
boolean test(T o);
}
class Filter{
public static <T> void filter(Collection<T> c,Predicate<T> p) {
if(null != c && null != p){
Iterator<T> itr = c.iterator();
while(itr.hasNext()){
T obj = itr.next();
if(!p.test(obj)){
itr.remove();
}
}
}
}
}
public class SimpleTest {
* @param args
*/
public static void main(String[] args) {
SimpleTest sdt = new SimpleTest();
sdt.filterCollection();
}
private void filterCollection() {
Integer[] iarr = {1,2,3,8};
List<Integer> list = new ArrayList<Integer>(Arrays.asList(iarr));
Filter.filter(list , new Predicate<Integer>() {
@Override
public boolean test(Integer o) {
return o.intValue() <= 5;
}
});
System.out.println(Arrays.toString(iarr));
System.out.println(list);
}
* output:
* [1, 2, 3, 8]
* [1, 2, 3]
*/
}