Why do you need to loop around the list to find out something about the Person instance - if you have a reference to Person, just use this. I can only assume that you had in mind the question:
How to find Person instances that match a specific condition?
If so, you can sort the list using a comparator that sorts the identity with isFemale () at a low level, so they go to the top of the list, then you can loop until person.isFemale () is false, for example:
List<Person> persons = ...; Collections.sort(list, new Comparator<Person>() { public int compare(Person o1, Person o2) { return o1.isFemale().compareTo(o2.isFemale()); // Note: Assumes isFemale returns Boolean, not boolean. Otherwise, wrap before comparing. } }); for (Person person : persons) { if (!person.isFemale()) { break; } // Do something with females }
Note. This is not a good idea, because it will be slower than just moving the list in the usual way, however I tried to answer the question as indicated.
source share