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
中的元素。