# Stream 接口一览 ## List 转 Stream ``` // 转stream list.stream() // 并发处理 list.parallelStream() ``` ## filter(过滤) ``` Stream filter(Predicate predicate); ``` ## map(元素转换) ``` Stream map(Function mapper); IntStream mapToInt(ToIntFunction mapper); LongStream mapToLong(ToLongFunction mapper); DoubleStream mapToDouble(ToDoubleFunction mapper); ``` ## flatMap(元素转换) ``` Stream flatMap(Function> mapper); IntStream flatMapToInt(Function mapper); LongStream flatMapToLong(Function mapper); DoubleStream flatMapToDouble(Function mapper); ``` ## distinct(去除重复,对象需要重写 equals、hashCode) ``` Stream distinct(); ``` ## sorted(排序) ``` Stream sorted(); Stream sorted(Comparator comparator); ``` ## peek(生成新的流:流是单向的,例如用于日志打印) ``` Stream peek(Consumer action); ``` ## limit(取前面 `n` 个元素) ``` Stream limit(long maxSize); ``` ## skip(跳过 `n` 个元素) ``` Stream skip(long n); ``` ## forEach(遍历) ``` void forEach(Consumer action); void forEachOrdered(Consumer action); ``` ## toArray(转换成数组) ``` Object[] toArray(); A[] toArray(IntFunction generator); ``` ## reduce(结果归并) ``` T reduce(T identity, BinaryOperator accumulator); Optional reduce(BinaryOperator accumulator); U reduce(U identity, BiFunction accumulator, BinaryOperator combiner); ``` ## collect(转换成集合) ``` R collect(Supplier supplier, BiConsumer accumulator, BiConsumer combiner); R collect(Collector collector); ``` ## 转list ``` // 转list Collectors.toList(); // 转set Collectors.toSet(); // 转map List testList = new ArrayList<>(10); Map data = releaseList.stream() .collect(Collectors.toMap(TestVo::getId, x -> x)); ``` ## count(计数) ``` long count(); ``` ## 查找 ``` boolean anyMatch(Predicate predicate); boolean allMatch(Predicate predicate); boolean noneMatch(Predicate predicate); ``` ## 查找 ``` Optional findFirst(); Optional findAny(); ```