Not quite sure how to word this question. I am wondering if there is a method to check certain parts of a custom java class to see if it matches a certain criteria. Such as this
public Name(String forename, String middlename, String surname)
And then when an array of instances of that class are created say,
Name[] applicants = new Name[4];
applicants[0] = new Name("john","bob", "rush");
applicants[1] = new Name("joe","bob", "rushden");
applicants[2] = new Name("jack","bob", "rushden");
applicants[3] = new Name("jake","bob", "rushden");
Is it possible to do a search over the instances of the class for person with
midddlename.equals("bob") && surname.equals("rush")
I am not really looking for a solution that is if(surname.equals("bob")) then else
,etc
But more a in-built java class that allows for rapid searching over the array. the speed of this is very important.
There isn't built in support, but Apache Collections and Google Collections both provide Predicate support over collections.
You may find this question and its answers helpful. Same with this developer.com article.
e.g. Using Google Collections:
final Predicate<name> bobRushPredicate = new Predicate<name>() {
public boolean apply(name n) {
return "bob".equals(n.getMiddlename()) && "rush".equal(n.getSurname());
}
}
final List<name> results = Iterables.filter(applicants, bobRushPredicate));
Java 8 added lambda expressions and the stream API, so support is built-in now.
Name[] applicants = new Name[4];
applicants[0] = new Name("john", "bob", "rush");
applicants[1] = new Name("joe", "bob", "rushden");
applicants[2] = new Name("jack", "bob", "rushden");
applicants[3] = new Name("jake", "bob", "rushden");
Optional<Name> result = Arrays.stream(applicants)
.filter(name -> name.middlename.equals("bob") && name.surname.equals("rush"))
.findAny();
result.ifPresent(name -> System.out.println(name));
There are lots of options available here. You can get the first name to match by switching .findAny()
to .findFirst()
or run the search in parallel by inserting .parallel()
after .stream(applicants)
, for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With