Instead of adding and updating these extra sequential number fields, I found useful two stage solution when processing well sorted multi-level collections for GUI (where row indexes are calculated on fly):
ParentEntity filterByParentAndChildListsIndexes(List<ParentEntity> parents, Integer prntIdx, Integer chldIdx){
AtomicInteger idx = new AtomicInteger(1); // may be '0'
Optional<ParentEntity> parentOp = parents.stream().filter(p -> idx.getAndIncrement() == prntIdx).findFirst();
if (parentOp.isPresent()){
return childFilter(parentOp.get(), chldIdx);
}
return null;
}
ChildEntity childFilter(ParentEntity parent, Integer chldIdx){
AtomicInteger idx = new AtomicInteger(1); // may be '0'
return parent.getChildEntities().stream().filter(c -> idx.getAndIncrement() == chldIdx).findFirst().orElse(null);
}
but it seems redundant and not flexible... so, I would be grateful for suggestions on existing solutions or suggestions for recursion of such multilevel filters.
You can do it like this:
Or just use
List.get():