zoukankan      html  css  js  c++  java
  • Java8 Stream流:集合的筛选、归约、分组、聚合

    一、概述

    在Java8中,使用Stream配合同版本出现的Lambda,给我们操作集合(Collection)提供了极大的便利。

    Stream将要处理的元素集合看作一种流,在流的过程中,借助Stream API对流中的元素进行操作,比如:筛选、排序、聚合等。

    二、Stream创建

    Stream流可以通过集合、数组来创建。

    List<String> list = Arrays.asList("a", "b", "c");
    Stream<String> stream = list.stream();
    

    三、使用

    3.1、遍历、匹配

    forEach

    List<Integer> list = Arrays.asList(10, 5, 8, 20, 32, 6);
    
    // 遍历输入每一个元素
    list.stream().forEach(item -> {
        System.out.println(item);
    });
    // 10
    // 5
    // 8
    // 20
    // 32
    // 6
    

    find

    List<Integer> list = Arrays.asList(10, 5, 8, 20, 32, 6);
    // 获取第一个值
    Integer integer = list.stream().findFirst().get();   // 10
    

    match

    List<Integer> list = Arrays.asList(10, 5, 8, 20, 32, 6);
    // list集合中是否有大于30的数
    boolean b = list.stream().anyMatch(i -> i > 30);   // true
    // list集合中是否有大于50的数
    boolean b2 = list.stream().anyMatch(i -> i > 50);   // false
    

    3.2、筛选

    filter

    List<Integer> list = Arrays.asList(10, 5, 8, 20, 7, 6);
    // 打印list集合中所有的奇数
    list.stream().filter(i -> i % 2 != 0).forEach(System.out::println);
    // 5
    // 7
    

    3.3、聚合

    max、min

    List<String> list = Arrays.asList("jack", "tom", "alice");
    // 取出list中最长的字符串
    String max = list.stream().max(Comparator.comparing(String::length)).get();   // alice
    String min = list.stream().min(Comparator.comparing(String::length)).get();   // tom
    

    count

    List<Integer> list = Arrays.asList(10, 5, 8, 20, 7, 6);
    // 获取list集合中偶数的个数
    long count = list.stream().filter(i -> i % 2 == 0).count();   // 4
    

    3.4、映射 

    map

    List<String> list = Arrays.asList("jack", "tom", "alice");
    // 名字全转大写
    List<String> list2 = list.stream().map(String::toUpperCase).collect(Collectors.toList());   // [JACK, TOM, ALICE]
    

    ---

    List<Integer> list = Arrays.asList(10, 5, 8, 20, 7, 6);
    // 每个都加100
    List<Integer> list2 = list.stream().map(i -> i + 100).collect(Collectors.toList());   // [110, 105, 108, 120, 107, 106]
    

    ---

    List<Student> list = Arrays.asList(
            new Student(1, "jack"),
            new Student(2, "alice"),
            new Student(3, "tom")
    );
    // 将对象列表转为字符串列表
    List<String> names = list.stream().map(student -> student.getName()).collect(Collectors.toList());   // [jack, alice, tom]
    

    3.5、归约

    reduce

    List<Integer> list = Arrays.asList(10, 5, 8, 20, 7, 6);
    // 求集合元素之和
    Integer num = list.stream().reduce(Integer::sum).get();   // 56
    

    3.6、收集(collect)

    toList

    List<Integer> list = Arrays.asList(10, 5, 8, 20, 7, 6);
    // 找出集合中的偶数,并返回新的集合
    List<Integer> newList = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toList());   // [10, 8, 20, 6]
    

    toSet

    List<Integer> list = Arrays.asList(10, 5, 8, 20, 7, 6);
    // 找出集合中的偶数,并返回新的集合
    Set<Integer> set = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toSet());   // [20, 6, 8, 10]

     参考:Java8 Stream:2万字20个实例,玩转集合的筛选、归约、分组、聚合

      

  • 相关阅读:
    UVA 10618 Tango Tango Insurrection
    UVA 10118 Free Candies
    HDU 1024 Max Sum Plus Plus
    POJ 1984 Navigation Nightmare
    CODEVS 3546 矩阵链乘法
    UVA 1625 Color Length
    UVA 1347 Tour
    UVA 437 The Tower of Babylon
    UVA 1622 Robot
    UVA127-"Accordian" Patience(模拟)
  • 原文地址:https://www.cnblogs.com/xulinjun/p/15267524.html
Copyright © 2011-2022 走看看