I'm reviewing some old code, where I'm grouping elements. It looks more or less like this:
Map<Long,List<Items>> groupedItems = ...
for (long groupid : groups){
for (Item item :items){
if (isGroupAccepting(item.getId(),groupid) || groupid == item.getGroup()) {
groupedItems.get(groupid).add(item);
}
}
}
I planned to replace it using grouping from stream API, but I'm stuck. It works fine for my second condition, but how to deal with the first one, where item should be added to every group which accepts that kind of item? Is it actually possible, or am I fighting a lost cause here?