Stream 流的创建
List<String> list = Arrays.asList("apple", "banana", "orange", "pear", "strawberry");
Stream<String> stream = list.stream();
stream.forEach(System.out::println);Stream 转换为其他数据类型
Stream 对基本数据类型的支持
最后更新于
List<String> list = Arrays.asList("apple", "banana", "orange", "pear", "strawberry");
Stream<String> stream = list.stream();
stream.forEach(System.out::println);最后更新于
// Stream 转换为数组
String[] arr = stream.toArray(String[]::new);// Stream 转换为 List
List<String> list = stream.collect(Collectors.toList());// Stream 转换为 Set
Set<String> set = stream.collect(Collectors.toSet());Map<String, String> map = stream.collect(Collectors.toMap(Function.identity(), Function.identity()));String str = stream.collect(Collectors.joining());Collection<String> collection = stream.collect(Collectors.toCollection(ArrayList::new));IntStream intStream = IntStream.of(1, 2, 3, 4, 5);
// 生成 0 到 100 的数字
IntStream.range(0, 100).forEach(System.out::println);
// 生成 0 到 100(包含 100)的数字
IntStream.rangeClosed(0, 100).forEach(System.out::println);
// 求和
int sum = intStream.sum();LongStream longStream = LongStream.of(1, 2, 3, 4, 5);
// 生成 0 到 100 的数字
LongStream.range(0, 100).forEach(System.out::println);
// 生成 0 到 100(包含 100)的数字
LongStream.rangeClosed(0, 100).forEach(System.out::println);DoubleStream doubleStream = DoubleStream.of(1.0, 2.0, 3.0, 4.0, 5.0);
// 生成 0 到 100 的数字
DoubleStream.iterate(0, n -> n + 1).limit(100).forEach(System.out::println);
// 生成 0 到 100(不包含 100)的数字
DoubleStream.iterate(0, n -> n + 1).limit(100).forEach(System.out::println);