我如何做内部Java 8流实例

for (List<Category> parentPath : getPathsInternal(parent, controlSet)) {
  if (!(parentPath instanceof LinkedList)) {
    parentPath = new LinkedList<Category>(parentPath);
  }
  parentPath.add(category);
  result.add(parentPath);
}


我不确定如何在Java 8流中编写此类功能。有方向吗?

if (!(parentPath instanceof LinkedList)) {
    parentPath = new LinkedList<Category>(parentPath);
  }

最佳答案

getPathsInternal(parent, controlSet).stream()
   .map(parentPath ->
       (parentPath instanceof LinkedList)
           ? parentPath : new LinkedList<>(parentPath))
   .peek(parentPath -> parentPath.add(category))
   .collect(toList()); // or whatever result is

08-17 23:11