不安分的黑娃
踏踏实实,坚持学习,慢慢就懂了~

参考资料

流式操作

1. java.util.stream.Stream 接口

Stream 代表一个支持串行或并行聚集操作的元素序列。

1.1 Stream 提供的方法

  • filter
    filter 生成一个新的 Stream,该 Stream 所有元素都满足 predicate.test = true 。
  • map
    map 根据 function 对所有元素进行计算,并返回一个 function 返回结果类型的 Stream。
  • flatMap
// flatMap 的入参是一个函数(入参类型为T,结果为 Stream的映射函数),返回一个元素类型为 R 的 Stream,flatMap 是个中间操作。
<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);
  • distinct()
    distinct 依据 Object.equals(Object) 对 Stream 的所有元素去重,返回去重后的 Stream
  • sorted
    sorted 对 Stream 所有元素按照自然顺序排序。还提供了重载方法支持传入一个比较器,用于排序。
  • peek
    peek 将 Stream 的元素送给 Consumer 消费使用,比如打印当前元素信息。

该方法主要用于 debug。

  • limit
    limit 限制Stream中元素数量。

limit 在串行流代价低廉,在并行流代价昂贵。

Stream<T> limit(long maxSize)
  • skip
    skip 跳过几个元素

skip 在串行流代价低廉,在并行流代价昂贵。

  • forEach
    forEach 轮询遍历 Stream 的元素,将元素送给 Consumer 消费使用。

  • toArray()
    转为数组。

  • reduce
    reduce 对 Stream 的所有元素,进行二元运算返回总结果。

// identity 恒值,accumulator 二元运算符
T reduce(T identity,
         BinaryOperator<T> accumulator)

相当于:

T result = identity;
     for (T element : this stream)
         result = accumulator.apply(result, element)
     return result;
  • collect
    collect 用于收集Stream中的元素,将每个元素收集到 R 中。
// supplier 用于创建返回结果类型的实例
// accumulator 将 Stream 的元素和supplier创建的实例进行计算
// combiner 将每个实例进行合并
<R> R collect(Supplier<R> supplier,
              BiConsumer<R,? super T> accumulator,
              BiConsumer<R,R> combiner)

相当于:

R result = supplier.get();
     for (T element : this stream)
         accumulator.accept(result, element);
     return result;

例子:

Stream<String> stringStream = Stream.of("1","2","3","4","5");
ArrayList list = stringStream.collect(ArrayList::new,ArrayList::add,ArrayList::addAll);
System.out.println(list.toString());
  • min
    返回 Stream 中最小元素。

  • max
    返回 Stream 中最大元素。

  • count
    Stream 中元素数量

  • anyMatch
    Stream 如果任一元素满足 Predicate 则返回 true。

  • allMatch
    Stream 如果全部元素满足 Predicate 则返回 true。

  • noneMatch
    Stream 如果全部元素都不满足 Predicate 则返回 true。

  • findFirst
    返回 Stream 的第一个元素

  • findAny()
    返回任一一个元素

  • iterator()
    继承自 BaseStream。

  • spliterator
    继承自 BaseStream。

  • isParallel
    是否是并行流

  • sequential
    返回串行流

  • parallel
    返回并行流

2.Collection 的流式操作

List<String> students = new ArrayList<>();
        students.add("小明");
        students.add("大薛");
        students.add("小白");
        students.add("大强");
        students.add("小胖");
        System.out.println(students.stream().toString());
        System.out.println("======= filter ======");
        List<String> bigStudents = students.stream().filter((student)->{
            if(student.contains("大"))
                return true;
            return false;
        }).collect(Collectors.toList());
        System.out.println(bigStudents.toString());
        System.out.println("======= flatMap ======");
        students.stream().flatMap((student)->{
             Map<String , String > studentFee = new HashMap<>();
             studentFee.put(student,"100元");
            return  Stream.of(studentFee);
        }).forEach((studentFee)->{
            System.out.println(studentFee.toString());
        });

结果:

======= filter ======
[大薛, 大强]
======= flatMap ======
{小明=100元}
{大薛=100元}
{小白=100元}
{大强=100元}
{小胖=100元}
posted on 2022-09-24 22:36  不安分的黑娃  阅读(88)  评论(0编辑  收藏  举报