This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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 ]
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.
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
Joined: Dec 04, 2000
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