I have an object Foo with the following elements:
class Foo {
int id;
int departmentId;
boolean condition1;
boolean condition2;
boolean condition3;
//...
}
and a list of Foo objects (~10k entries):
List<Foo> fooList = new ArrayList<>();
fooList.add(...);
//...
I need to iterate through each of the departmentIds of this list, and be able to stop any further iterations of a particular departmentId once its objects meet a particular combination of conditions.
For this purpose, I was thinking to simply create a new Map which holds my departmentId as a key and all related Foo objects as its value. So that I could iterate through my new objects based on the departmentId, and easily stop the iteration for other departments with same Id once the condition is met. Something like:
Map<Foo.departmentId, List<Foo>> departmentFoos = new HashMap<>();
Can this be achieved in a better way other than iterating through my fooList and putting/replacing the object of my HashMap one by one?
Set, do you actually meanMap?Mapthat you are looking for usingCollectors.groupingBywhile streaming the initialList<Foo>. After which, you can iterate over the (key, val) pairs and iterate further on thevalto findanyMatchand break further traversal.