Stream轉爲其他數據結構

Stream<String> stream = Stream.of("a","b", "c");

//轉爲數組

String[] strArray1 = stream.toArray(String[]::new);

// 轉爲 Collection 的實現類

List<String> list1 = stream.collect(Collectors.toList());

List<String> list2 = stream.collect(Collectors.toCollection(ArrayList::new));

Set set1 = stream.collect(Collectors.toSet());

Stack stack1 = stream.collect(Collectors.toCollection(Stack::new));

// 轉爲String

String str = stream.collect(Collectors.joining()).toString();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章