Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collection Filtering

 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of Collections?
[ April 10, 2003: Message edited by: Michael Morris ]
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um, you know... collections collections; like from java.util. Things like Set, List, Map, etc.
--Mark
 
Garandi Garandi
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashMap might be useful.
Garandi
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic