Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

filter java.util.List based some requirements.

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while I am doing a java.util.List, I always need to filter the List based some requirements.

For example , I have a People object which has "name" and "age" attributes. Now, question is , how to get all Peoples, in the a List, whose ages equals to "30" ?

if we have a customized List.indexOf(obj, comparator), it will be better. or we have some open source java List API to solve this problem ?

Thanks.
 
Junilu Lacar
Bartender
Pie
Posts: 7319
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know if it helps you but I found these:

http://www.javapractices.com/Topic163.cjp
http://www.publicobject.com/glazedlists/
 
Junilu Lacar
Bartender
Pie
Posts: 7319
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jakarta Commons Collection also has the following classes you might want to look at:

org.apache.commons.collections.iterators.FilterIterator
org.apache.commons.collections.iterators.FilterListIterator

See http://jakarta.apache.org/commons/collections/apidocs/index.html
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

But I can't find an example how to use Apache Collections FilterIterator . Apache web site can't find it, either.

Thanks.
 
Junilu Lacar
Bartender
Pie
Posts: 7319
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a small unit test to illustrate the use:

 
Junilu Lacar
Bartender
Pie
Posts: 7319
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be easy enough for you to replace the anonymous Predicate class with something like an AgePredicate:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic