本文介绍了如何产生J2SE 8的流, 包括基本类型的流IntStream, LongStream, DoubleStream 。

展现流的方法

public static <T> void show(String title, Stream<T> stream){
System.out.println("title:"+title); List<T> collect = stream.limit(10).collect(Collectors.toList());
collect.forEach(n->System.out.println(n));
System.out.println();
}

创建公共的list

ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("aa");
arrayList.add("bb");
arrayList.add("cc");

1. 产生流方式一  List.stream()   List->流

Stream<String> listStream = arrayList.stream();
show("listStream", listStream);

2. 产生流方式二  List.parallelStream() 并行流,可以在多线程中执行操作  List->流

Stream<String> parallelListStream = arrayList.parallelStream();
show("parallelListStream", parallelListStream);
不想出现流被关的错误,用Supplier<T> 包装流,直接get(),多次使用不会错

Supplier<Stream<String>> arrayListSupplier = () -> arrayList.stream();
show("arrayListSupplier", arrayListSupplier.get());
show("arrayListSupplier", arrayListSupplier.get());
show("arrayListSupplier", arrayListSupplier.get());

3. 产生流方式三  Pattern方式  字符串切割->流

Stream<String> patternStream = Pattern.compile("\r\n").splitAsStream("11111\r\n22222\r\n33333\r\n6666666666666");
show("patternStream", patternStream);

4. 产生流方式四  Files方式  文件->流

Stream<String> fileStream = Files.lines(Paths.get("E:\\111.txt"),StandardCharsets.UTF_8);		//StandardCharsets.UTF_8
show("fileStream", fileStream);

5. 产生流方式五  Arrays.stream()  数组->流

Stream<String> arrayStream = Arrays.stream(new String[]{"AA","BB","CC"});
Arrays.stream(new String[]{"AA","BB","CC"},0,2); //T[] array, int startInclusive, int endExclusive 切割数组
show("arrayStream", arrayStream);

6. 产生流方式六  Stream.of()   任意对象->流

Stream.of(arrayList);
Stream<String> ofStream = Stream.of("aa","bb","cc");
show("ofStream", ofStream);

7. 产生流方式七  Stream.empty()  创建空流

Stream<Object> emptyStream = Stream.empty();
show("emptyStream", emptyStream);

8. 产生流方式八  Stream.generate()  产生无限流 无限长度的Stream,其元素的生成是通过给定的Supplier

Stream<String> generateStream1 = Stream.generate(()->"AABBCC");
show("generateStream1", generateStream1); Stream<Double> generateStream2 = Stream.generate(new Supplier<Double>() {
@Override
public Double get() {
return Math.random();
}
}); generateStream2 = Stream.generate(()->Math.random());
generateStream2 = Stream.generate(Math::random); show("generateStream2", generateStream2);

9. 产生流方式九  iterate()    接受种子+函数

Stream<BigInteger> iterateStream = Stream.iterate(BigInteger.ZERO, n->n.add(BigInteger.ONE));
show("iterateStream", iterateStream);

10. 产生流方式十  IntStream

IntStream intStream = IntStream.of(1,1,2,3,4);
show("intStream", intStream.boxed()); //boxed() 基本类型流->对象流 intStream = IntStream.range(0, 5); //[int startInclusive, int endExclusive),不包括5
show("intStream", intStream.boxed()); intStream = IntStream.rangeClosed(0, 5); //[int startInclusive, int endExclusive],包括5
show("intStream", intStream.boxed()); intStream = IntStream.generate(()->new Random().nextInt());
show("intStream", intStream.boxed()); intStream = IntStream.iterate(0, n->n+8);
show("intStream", intStream.boxed()); IntStream mapToIntStream = arrayList.stream().mapToInt(String::length); //mapToInt 将对象流->基本类型流
show("mapToIntStream", mapToIntStream.boxed());

11. 产生流方式十一  LongStream

LongStream longStream = LongStream.of(1,1,2,3,4);
show("longStream", longStream.boxed()); //boxed() 基本类型流->对象流 longStream = LongStream.range(0, 5); //[int startInclusive, int endExclusive),不包括5
show("longStream", longStream.boxed()); longStream = LongStream.rangeClosed(0, 5); //[int startInclusive, int endExclusive],包括5
show("longStream", longStream.boxed()); longStream = LongStream.generate(()->new Random().nextLong());
show("longStream", longStream.boxed()); longStream = LongStream.iterate(0, n->n+8);
show("longStream", longStream.boxed()); LongStream mapToLongStream = arrayList.stream().mapToLong(x->(long)x.length()); //mapToLong 将对象流->基本类型流
show("mapToLongStream", mapToLongStream.boxed());

12. 产生流方式十二  DoubleStream

DoubleStream doubleStream = DoubleStream.of(1.1,1.2,1.3,2.1,2.2);
show("doubleStream", doubleStream.boxed()); doubleStream = DoubleStream.generate(Math::random);
show("doubleStream", doubleStream.boxed()); doubleStream = DoubleStream.iterate(1.1, n->n+1.1);
show("doubleStream", doubleStream.boxed()); DoubleStream mapToDoubleStream = arrayList.stream().mapToDouble(x->(double)x.length()); //mapToDouble 将对象流->基本类型流
show("mapToDoubleStream", mapToDoubleStream.boxed());


13. 对应的,创建short,char,byte,boolean,float流

Stream<Boolean> booleanStream = Arrays.stream(new Boolean[]{true,false,false,true});
show("booleanStream", booleanStream);

14.  流操作, 不要修改流操作中间引出的集合的值

//不在流中修改arrayList,不要紧
Stream<String> stream = arrayList.stream();
arrayList.add("added stream");
System.out.println(stream.count()); //在流中修改arrayList,会 java.util.ConcurrentModificationException
//arrayList.stream().forEach(s->arrayList.remove(s));

15. 并行流

使用并行流的条件:
1).操作可以以任意顺序执行, 顺序执行结果和不顺序执行的结果相同
2).操作可以分开执行,并行执行
//生成并行流的两种方式
Stream<String> parallelStream = arrayList.parallelStream();
parallelStream = arrayList.stream().parallel(); Map<Integer, Long> parallelStreamResult = parallelStream.filter(w->w.length()>1).collect(Collectors.groupingBy(String::length, Collectors.counting()));
parallelStreamResult.entrySet().forEach((n)->System.out.println(n.getKey()+" , counting:"+n.getValue())); //unordered表示对排序不感兴趣, 打乱顺序
arrayList.parallelStream().limit(2).forEach((n)->System.out.println(n));
System.out.println();
arrayList.parallelStream().unordered().limit(2).forEach((n)->System.out.println(n));





05-11 21:44