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();

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