流的概念和作用
流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。
Java流类图结构:
Stream流的聚合操作
1.末端方法;都是一次性的使用,流就会关闭,不能再用此流了。
2.中间方法;会返回一个新的流对象,继续调用其他的聚合方法操作。
1 public class TestStream1 { 2 3 public static void main(String[] args) { 4 //存整数 生产者模式 思想 5 //返回 一个 流对象 6 IntStream stream = IntStream.builder().add(11).add(22).add(33).add(44).build(); 7 // System.out.println(stream.max().getAsInt());//末端 8 // IntStream stream1 = IntStream.builder().add(11).add(22).add(33).add(44).build(); 9 // System.out.println(stream1.min().getAsInt()); 10 // System.out.println(stream.min().getAsInt()); 11 // System.out.println(stream.sum()); 12 // System.out.println(stream.average().getAsDouble()); 13 // System.out.println(stream.count()); 14 //是否 所有的 数据 都 满足条件,都满足 true,否则不成立 15 /* System.out.println(stream.allMatch(new IntPredicate() { 16 17 @Override 18 public boolean test(int value) { 19 // TODO Auto-generated method stub 20 return value > 10; 21 } 22 }));*/ 23 // System.out.println(stream.allMatch(v-> v > 10)); 24 //只要有一个元素满足条件 就 成立 true 25 // System.out.println(stream.anyMatch(v-> v > 22)); 26 //中间方法 27 // System.out.println(stream.filter(v-> v > 22).count()); 28 stream.filter(v->v>22).forEach(System.out::println); 29 30 31 } 32 33 34 35 }