Stream转为其他数据结构

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

//转为数组

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

// 转为 Collection 的实现类

List list1 = stream.collect(Collectors.toList());

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

你可能感兴趣的:(JAVA)