list.stream.filter,List<List>转换为List
1.filter过滤
返回符合查询条件的集合//过滤所有deviceType为1的List<DeviceWorkTimeEntity> list= entities.stream().filter(a -> "1".equals(a.getDeviceType())).toList();
2.List<List>
转换为List
可以使用流(Stream)的`flatMap`操作
public class Example {public static void main(String[] args) {List<List<String>> nestedList = new ArrayList<>();nestedList.add(Arrays.asList("A", "B", "C"));nestedList.add(Arrays.asList("D", "E"));nestedList.add(Arrays.asList("F", "G", "H", "I"));List<String> flatList = nestedList.stream().flatMap(List::stream).collect(Collectors.toList());System.out.println(flatList);}
}
在这个示例中,我们首先创建了一个嵌套的List
对象nestedList
,其中包含了多个List
。然后,我们使用流的flatMap
操作将嵌套的List
展开为一个平铺的List
,最后使用collect
方法将结果收集到一个新的List
对象flatList
中。最后,我们打印出flatList
的内容。
运行以上代码,你将会得到一个平铺的List
,其中包含了所有嵌套List
中的元素。