Best Practice for Defining Collection Filters in a Spring Context File

343 Views Asked by At

Whats the best practice for defining filters to apply to a collection of objects within a spring context file?


Say I have a Collection of Persons (Collection<Person> people). Each person has age/name/sex/occupation/email properties.

I need to create subgroups of these people, based on some standard & simple predicates. Example groupings may be:

  1. sex=Male
  2. sex=Female && age>30 && occupation=IT
  3. sex=Male && occupation=IT && email endsWith "aol.com"

I can see a relatively straight forward route to doing this with Guava Predicates & Collections2.filter() by defining Predicate(s) in code, pulling them into collections in context file end injecting appropriate collection of Predicates where needed.

JFilter is a possible contender too, as you can define filters as Strings. LambdaJ seems to be the other popular option for filtering Collections in Java, but not obvious how to allow filtering logic to be driven from a Spring Context.


Is there a better way to go about this?

Note, I want to a avoid call to database each time, as I already have the collection of objects I care about in memory.

0

There are 0 best solutions below