I have list List
where Custom
is like
class Custom{
public int id;
public String name;
}
How to
Personally, I like using the Apache Commons Collection lib when I can. (But the one on sourceforge since it uses generics) It lets you do some pretty elegant things such as mapping lists or filtering lists (in a scheme-ish way). You would end up writing something like this:
int count = CollectionUtils.countMatches(myList, new Predicate(){
public boolean evaluate(Custom c){ return "Tom".equals(c.name); }
}
The only downside is that since Java doesn't have first-order functions, you have to write little objects like that Predicate instance. It would be cleaner if you could write anonymous functions. i.e. in scala, it would be this:
val myList = List("a", "a", "b", "c", "a")
val c = myList.count{ "a".equals(_) } // is 3