List<Cat> bigList = ....; // master list Collection<Cat> smallList = CollectionUtils.select(bigList, new Predicate() { public boolean evaluate(Object o) { Cat c = (Cat)o; return c.getFavoriteFood().equals("Wiskas") && c.getWhateverElse().equals(Something); } });
Predicate condition = new Predicate() { boolean evaluate(Object sample) { return ((Sample)sample).value3.equals("three"); } }; List result = CollectionUtils.select( list, condition );
Customer findCustomerByid(final int id){ return (Customer) CollectionUtils.find(customers, new Predicate() { public boolean evaluate(Object arg0) { return ((Customer) arg0).getId()==id; } }); }
Collection<SomeObject> selectedObjects = CollectionUtils .select(list, new Predicate() { boolean evaluate(Object input) { return ((SomeObject) input).someValue().equals(value); } });
Predicate even = new Predicate() { public boolean evaluate(Object object) { return ((Integer)object) % 2 == 0; } }
Predicate test1 = new Predicate() { @Override public boolean eval(int a, int b) { return a >= b; } }; // etc.
Predicate inactiveCompaniesPredicate = new Predicate() { @Override public boolean apply(PredicateContext ctx) { if(ctx.item(Map.class).containsKey("errorMsg") && ((JSONArray)((JSONArray) ctx.item(Map.class).get("errorMsg")).get(0)).get(0).equals("INACTIVE")) { return true; } else { return false; } } };
Iterables.all(collection, new Predicate() { boolean apply(T element) { .... //check your condition } }
List<Toy> toys; List<Toy> playstations = Collections2.filter( toys, new Predicate() { boolean apply(TOY toy){ return toy == TOY.PLAYSTATION; } });
Collection matches = CollectionUtils.select(collection, new Predicate() { public boolean evaluate(Object object) { // evaluate predicate } });
public static Predicate ALL = new Predicate(){ @Override boolean accepts(Object in) { return true; } }; @SuppressWarnings("unchecked") static final <T> Predicate<T> all(){ return (Predicate<T>)ALL;}
void foo(final int n) { CollectionUtils.filter(collection, new Predicate() { public boolean evaluate(Object input) { return input.equals(n); } }); }
List<Person> filteredList = new ArrayList<Person>(allPersons); CollectionUtils.filter( filteredList, new Predicate() { boolean evaluate(Object object) { //do whatever you want } });
boolean contains = CollectionUtils.exists(listFoo, new Predicate() { public boolean evaluate(Object input) { return "Baz".equals(((Foo)input).getBar()); } });
final Iterator iterator1 = ... final Iterator iterator2 = ... Iterator iterator3 = IteratorUtils.filteredIterator(iterator1, new Predicate() { private List list = IteratorUtils.toList(iterator2); @Override public boolean evaluate(Object object) { return !list.contains(object); } });
ArrayList haystack = // ... final Object needleField1 = // ... final Object needleField2 = // ... Object found = CollectionUtils.find(haystack, new Predicate() { public boolean evaluate(Object input) { return needleField1.equals(input.field1) && needleField2.equals(input.field2); } });
Set<Field> fields = ReflectionUtils.getAllFields(X.class, new Predicate() { public boolean apply(Object input) { return true; }}); Map<Field, Object) values = ... for(Field f : fields) { f.setAccessible(true); values.put(f, f.get(obj); }
public static void main(String[] args) { List<Integer> aList = new ArrayList<Integer>(Arrays.asList(1,2,3)); @SuppressWarnings("unchecked") Collection<Integer> view = Collections2.filter(aList, new Predicate() { public boolean apply(Object arg0) { return ((Integer) arg0).intValue() % 3 != 0; }}); view.add(new Integer(4)); view.remove(new Integer(2)); System.out.println("aList: " + aList); System.out.println("view : " + view); }
Predicate equalsOne = new Predicate() { public void test(String x) { return "1".equals(x); } }; Function<ErrorCodeUtil> actionOne = new Function<ErrorCodeUtil>() { public void apply(ErrorCodeUtil t) { t.handleErrorCode1(); } } decisionTable.put(equalsOne, actionOne);
Dog someDog = new Dog(); Dog dog = CollectionUtils(dogList, new Predicate() { @Override public boolean evaluate(Object o) { Dog d = (Dog)o; return someDog.getName().equals(d.getName()); } });