Reputation: 15
I have complex objects
class Person{
Integer id;
String fname;
String lname;
//..... there are more attributes
List<Address> addresses;
}
class Address{
String street1;
String street2;
String city;
String state;
String zip;
}
I have about 30000 Person objects and each has at-least 200-500 sub object (addresses). Now I have all these in-memory and i have different scenarios (20-30 scenarios) where i have to query/filter objects based on filters available in scenarios. For eg.
In short, there are multiple scenarios with different combination of both the objects. Please advice the best way to utilize Guava for such case?
Upvotes: 2
Views: 1732
Reputation: 35467
You should use Iterables.filter(Iterable, Predicate)
in conjuction with Predicate
s.
Example:
public class PersonLastNameEqualsPredicate implements Predicate<Person> {
private String personName;
public PersonNameEqualsPredicate (String personName) {this.personName= personName;}
public boolean apply(Person p) { return this.personName.equals(p.getLName()); }
}
public class PersonStreet1EqualsPredicate implements Predicate<Person> {
private String street1Name;
public PersonStreet1EqualsPredicate (String street1Name) {this.street1Name = street1Name;}
public boolean apply(Person p) {
for (Address a: p.getAddresses()) {
if (street1Name.equals(a.getStreet1()) return true;
}
return false;
}
}
// Extra predicates as you need them
You need one predicate per scenario item. Then you need to modulate them as you need them:
1.
Predicate<Person> peopleNamedZoik = new PersonLastNamePredicate("zoik");
2.
Predicate<Person> peopleNamedSmarkAndLivingInXyzStreet = Predicates.and(new PersonLastNamePredicate("smark"), new PersonStreet1EqualsPredicate("xyz"));
3.
Here you should adapt your street predicate to check at once if all street names contain all your expectation.
Then it's simply a matter of
1.
List<Person> myLongPersonList = ...;
Iterable<Person> zoikPeople = Iterables.filter(myLongPersonList, peopleNamedZoik);
2.
List<Person> myLongPersonList = ...;
Iterable<Person> zoikPeople = Iterables.filter(myLongPersonList, peopleNamedSmarkAndLivingInXyzStreet);
And so on.
Upvotes: 3