In the following method I want to replace the forEach loop with a stream.
protected Set<String> getSelectedPaths(final CheckModel<TreeItem<String>> checkModel, final LazyTreeItem root) {
final Set<String> ceckedList = new HashSet<>();
final List<TreeItem<String>> children = root.getChildren();
final List<LazyTreeItem> lazyChildren = children.stream().map((item -> (LazyTreeItem) item))
.collect(Collectors.toList());
final List<String> selectedChildren = new ArrayList<>();
children.forEach(child -> selectedChildren.addAll(getSelectedPaths(checkModel, (LazyTreeItem) child)));
/**
* do sth with checkedList
*/
return checkedList
the lines
final List<String> selectedChildren = new ArrayList<>();
children.forEach(child -> selectedChildren.addAll(getSelectedPaths(checkModel, (LazyTreeItem) child)));
should be replaced with a stream. I tried with
final List<String> selectedChildren = children.stream().map(child->getSelectedPaths(checkModel, (LazyTreeItem) child)).collect(Collectors.toList());
but the compiler error "
Type mismatch: cannot convert from List<Set<String>> to List<String>"
is thrown.
Is there an opportunity to replace this forEach loop with a stream?