stream流
Stream
1 获取Stream流
使用Collection中的stream()生成流 如list(集合对象).stream()
双列集合间接获取 先通过 keySet或者 entrySet 获取set 在获取stream流
数组 通过数组的帮助类 Arrys.stream(数组名)
相同数据类型的多个元素 通过Stream类 中的 of()方法 stream.of()
2 中间方法
forEach () 自动遍历流对象中的数据。将每一个数据一次传递给lambda表达式
方法的参数: Consumer 接口, 抽象方法 void accept(T t)
filter方法(对stream中的元素进行筛选,返回新的的stream对象)获取流中的每一个数据。test 方法中的s,依此便是流中的每一个数据
是就返回true留下。
stream().filter(s->s.startWith("王")) 留下开头为王的数据,返回一个新的stream对象
Stream<T> limit(long maxSize) 截取指定参数个数的数据
Stream<T> skip(long n) 跳过指定参数个数的数据
static Stream<T> concat(Stream a, Stream b) 合并a和b
Stream<T>distinct() 去除流中的重复数据(依赖于 hashCode和equals方法)
3 终结方法
forEach(Consumer action)对此流的每个元素进行操作
Consume接口中的 void accept(Y t)对给定参数执行此操作
count()返回流中元素的个数
收集系列
R collect(Collector collector) //收集流中的数据,不创建容器,不把数据添加到容器中
Collectors中的静态方法
例 stream.collect(Collector.toList())
toList()把元素收集到List
toSet()
toMap (key ,value)