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

Collection Filtering

Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
I'm looking for an efficent way to filter a collection. We just moved some database tables into memory, holding the data in Collections. Now we need filtering to replace EJB/SQL finders (e.g. find all elements of the collection where the name field equals "Bob").
--Mark
[ April 10, 2003: Message edited by: Mark Herschberg ]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
What kind of Collections?
[ April 10, 2003: Message edited by: Michael Morris ]

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
Um, you know... collections collections; like from java.util. Things like Set, List, Map, etc.
--Mark
Garandi Garandi
Ranch Hand

Joined: Jan 07, 2003
Posts: 192
HashMap might be useful.
Garandi
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

Um, you know... collections collections; like from java.util. Things like Set, List, Map, etc.

I was hoping you would say something definite like Maps using the primary key, even though a Map is not a collection. So you just want use a generic Collection. I guess it really all depends on how many findBys you have, the number of fields in each collection, whether and how the collection is sorted and the collection type.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Don't forget this previous discussion for fairly arbitrary types of "filtering". For more specific advice, yeah it depends on a lot of different things. If you want to do a lot of searches quickly where you're searcing for some sort of exact match for a key, a HashMap is the way to go. It just takes a bit of time to set it up in the first place, putting in all the key-value pairs. If you want to find some sort of range, eg all names alphabetically between "Jim" and "Mark", then you'd need a TreeMap using the name as a key. Slower than HashMap, but you can extract an ordered subset of the mappings. For other stuff, like say matching a field against an arbitrary regular expression, you may have to just iterate through a collection an apply a check to whatever field you're searching on. Or design some clever custom data structure that makes your particular search easier. Lots of different possibilities...


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

Joined: Dec 04, 2000
Posts: 6037
You know what, I got it totally backwards. I was thinking of things working like the comparator method, and was wondering how to put 6 different comparators into a single class. But the way to do it is to have 6 different predicate objects. Duh!
Thanks.
--Mark
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Do not forget to check out the Jakarta Commons Collections. Closures, predicates, all kinds of stuff that you might miss in the Java Collections framework.
- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collection Filtering
 
Similar Threads
destroying objects
Same old song, same old dance, Lazy Hibernate
A question about criteriaFind metod
Jar File Creation Destroys Images in Maven2
Collections Methods needed for exam ?