Two Laptop Bag*
The moose likes Beginning Java and the fly likes filter  java.util.List based some requirements.   Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "filter  java.util.List based some requirements.   " Watch "filter  java.util.List based some requirements.   " New topic
Author

filter java.util.List based some requirements.

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
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

Joined: Feb 26, 2001
Posts: 4456
    
    6

Don't know if it helps you but I found these:

http://www.javapractices.com/Topic163.cjp
http://www.publicobject.com/glazedlists/


Junilu - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

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

Joined: Dec 23, 2003
Posts: 798
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

Joined: Feb 26, 2001
Posts: 4456
    
    6

Here's a small unit test to illustrate the use:

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

It would be easy enough for you to replace the anonymous Predicate class with something like an AgePredicate:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: filter java.util.List based some requirements.