permaculture playing cards*
The moose likes Java in General and the fly likes Filtering Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Filtering Collections" Watch "Filtering Collections" New topic
Author

Filtering Collections

Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
I need to filter a Java Collection. Before I start reinventing the wheel, is there any standard way of doing this? Better yet, any small open source utility packages out there?
--Mark
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I'm not sure what you mean here. Will this do:

where shouldBeRemoved(Object) is where you define the criteria for filtering. You can make this configurable by creating some sort if filtering criteria object, similar to FilenameFilter as used by File's list() method.
Depending on the situation you may also wish to leave the original Collection intact, and create a filtered copy:

Is this what you're looking for?
[ February 24, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
Yeah, that's what I did. Of course, it's a very focused filter. Right now I'm filtering on an ID tag. Later I might want to filter on a date (either instead of, or additionally). So then I started envisioning interfaces and generic filters, etc. I was just wondering if there's a standard pattern for this. Right now I've got things like
Interfaces:
  • SecurityFilterablepublic long getSecurityID()
  • DateFilterable

  • public long getTimestamp()

    (The names are terrible, any better ideas?)
    And then a filter class with methods like filterOnSecurityID(Iterator iter, long id) and filterOnDate(Iterator iter, long start, long end).
    Of course, I can make this more generic (but probably at an performance cost), by passing in not just an iterator, by some type of comparator. In some sense, I'm basically sorting, and then trunkating the collection.
    I was just wondering if there was some standard way of doing this, so I don't reinvent the wheel.
    --Mark
    [ February 24, 2003: Message edited by: Mark Herschberg ]
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    I don't know of standard named patterns for this sort of thing, but that may be just because I don't spend enough time in our Patterns forum.
    Of course, I can make this more generic (but probably at an performance cost),
    Ah, you worry too much about performance at this point. (Well, probably.)
    Usual practice with collections is to pass a Collection (or List/Set/whatever) rather than an Iterator. You can easily get an Iterator from a Collection, but it's more work to go the opposite direction.
    For more generic filtering I'd probably go with something like this:

    And then to use it:

    Just off the top of my head; may or may not be a good fit for your project.
    Mark Herschberg
    Sheriff

    Joined: Dec 04, 2000
    Posts: 6037
    Maybe I am misunderstanding the relationship between iterators and collections. When you pass in a collection, and modify it's iterator (e.g. remove objects from it), you're not modifying the underlying collection object, and so unless you create a new iterator with the non-removed items, you'll lose those changes.
    On the other hand, passing in an iterator, and then modifying it, will result in having something filtered when the method returns, right?

    --Mark
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    When you pass in a collection, and modify it's iterator (e.g. remove objects from it), you're not modifying the underlying collection object,
    No, Iterator's remove() does affect the underlying Collection. From the API: "Removes from the underlying collection the last element returned by the iterator" The Iterator is a view of the Collection, not just a copy.
    [ February 24, 2003: Message edited by: Jim Yingst ]
    Peter den Haan
    author
    Ranch Hand

    Joined: Apr 20, 2000
    Posts: 3252
    Look into the Jakarta Commons Collections, in particular FilterIterator, Predicate and CollectionUtils.filter(Collection, Predicate). I'm quite disappointed that closures and predicates aren't part of the Java Collections framework, to be honest.
    - Peter
    [ February 25, 2003: Message edited by: Peter den Haan ]
    Mark Herschberg
    Sheriff

    Joined: Dec 04, 2000
    Posts: 6037
    That's what I love about JavaRanch, I'm always learning new things. :-)
    Thanks Jim and Peter.
    --Mark
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Filtering Collections
     
    Similar Threads
    Sun Study Guide
    redirect within a servlet
    Timers
    Jar all the servlets?
    need MAC address of client machine in servlet