Stream & JDK8新特性

2022-11-11 18:46:20 浏览数 (1)

常用函数式接口

Function<T,R> 转换型:接受一个输入参数,返回一个结果

Consumer<T> 消费型:接受一个输入参数,无返回

Predicate<T> 判断型:接受一个输入参数,返回Boolean结果

Supplier<T> 供给型:无参数,返回结果

Pojo UserInfo

代码语言:javascript复制
public class UserInfo {
    private Integer id;
    private String name;
    private Integer isDelete;

    public UserInfo(Integer id, String name, Integer isDelete) {
        this.id = id;
        this.name = name;
        this.isDelete = isDelete;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getIsDelete() {
        return isDelete;
    }

    public void setIsDelete(Integer isDelete) {
        this.isDelete = isDelete;
    }

    @Override
    public String toString() {
        return "UserInfo{"  
                "id="   id  
                ", name='"   name   '''  
                ", isDelete="   isDelete  
                '}';
    }

}

List -> Map

代码语言:javascript复制
    public static void main(String[] args) {
        ArrayList<UserInfo> list = new ArrayList();
        list.add(new UserInfo(1, "张三", 1));
        list.add(new UserInfo(2, "李四", 2));
        list.add(new UserInfo(3, "王五", 3));
        list.add(new UserInfo(3, "赵六", 4));
        Map<Integer, UserInfo> userInfoMap = list.stream().collect(
                // Collectors.toMap参数是3个lambda表达式:生成键的映射函数、生成值的映射函数、合并函数(因为Map存储可能会重复,导致元素丢失,我们需要指定规则保留哪个)
                Collectors.toMap(
                        (UserInfo::getId), (userInfo -> userInfo), (k1, k2) -> {
                            // 合并函数,如果ID相同,保留isDelete的元素
                            if (k1.getIsDelete() > k2.getIsDelete()) {
                                return k1;
                            } else {
                                return k2;
                            }
                        })
        );
        userInfoMap.values().forEach(k -> {
            System.out.println(String.format("name=%s,isDelete=%s",k.getName(),k.getIsDelete()));
        });

Filter 过滤

代码语言:javascript复制
public static void main(String[] args) {
    ArrayList<UserInfo> list = new ArrayList();
    list.add(new UserInfo(1, "张三", 1));
    list.add(new UserInfo(2, "李四", 2));
    list.add(new UserInfo(3, "王五", 3));
    list.add(new UserInfo(3, "赵六", 4));
    List<UserInfo> collect = list.stream()
            // 保留isDelete > 2 的元素
            .filter(userInfo -> userInfo.getIsDelete() > 2)
            .collect(Collectors.toList());
    collect.forEach(k -> {
        System.out.println(String.format("name=%s,isDelete=%s", k.getName(), k.getIsDelete()));
    });
    // 只获取保留元素ID列表
    LinkedList<Integer> collect1 = collect.stream()
            .map(UserInfo::getId)
            .collect(Collectors.toCollection(LinkedList::new));
    System.out.println("获取到的ID列表是"   collect1);
}

GroupingBy分组

我们日常SQL使用的时候,都是Group By实现分组。Stream也有类似功能,比如一个集合有3个组的员工,我们想获取每个组的员工,就可以通过.collect(Collectors.groupingBy(Obj::getXXX));实现。返回的集合是Map集合!

代码语言:javascript复制
public static void main(String[] args) {
    ArrayList<UserInfo> list = new ArrayList();
    list.add(new UserInfo(1, "张三", 1));
    list.add(new UserInfo(2, "李四", 2));
    list.add(new UserInfo(3, "王五", 3));
    list.add(new UserInfo(3, "赵六", 4));
    Map<Integer, List<UserInfo>> collect = list.stream()
            .collect(Collectors.groupingBy(UserInfo::getId));
    collect.forEach((k,v) -> {
        System.out.println(String.format("GroupID=%s,Element=%s",k.toString(),Arrays.toString(v.toArray())));
    });
}

排序 sorted Comparator

代码语言:javascript复制
    public static void main(String[] args) {
        ArrayList<UserInfo> list = new ArrayList();
        list.add(new UserInfo(1, "张三", 1));
        list.add(new UserInfo(2, "李四", 2));
        list.add(new UserInfo(3, "王五", 3));
        list.add(new UserInfo(3, "赵六", 4));
        List<UserInfo> collect = list.stream()
                .sorted(Comparator
                        .comparing(UserInfo::getId)
                        .thenComparing(UserInfo::getIsDelete, Comparator.reverseOrder())  // 默认升序,多一个参数实现此字段倒序
                        // .reversed() // 实现所有条件排序后,再反转排序
                )
                .collect(Collectors.toList());
        collect.forEach(k -> {
            System.out.println(String.format("id=%s,name=%s,isDelete=%s", k.getId(), k.getName(), k.getIsDelete()));
        });
    }

Distinct 对元素去重

代码语言:javascript复制
public static void main(String[] args) {
    ArrayList<String> list = new ArrayList();
    list.add("A");
    list.add("A");
    list.add("B");
    list.add("A");
    List<String> collect = list.stream().distinct().collect(Collectors.toList());
    collect.forEach(k -> {
        System.out.println(String.format("Element=%s",k));
    });
}

FindFirst 返回第一个元素

代码语言:javascript复制
    public static void main(String[] args) {
        ArrayList<UserInfo> list = new ArrayList();
        list.add(new UserInfo(1, "张三", 1));
        list.add(new UserInfo(2, "李四", 2));
        list.add(new UserInfo(3, "王五", 3));
        list.add(new UserInfo(3, "赵六", 4));
        list.stream().findFirst().ifPresent(System.out::println);

        UserInfo userInfo = list.stream().findFirst().get();
        System.out.println(userInfo);
    }

AnyMatch 与 AllMatch

AnyMatch匹配整个集合是否包含某元素

AllMatch匹配整个集合是否都是某元素

代码语言:javascript复制
public static void main(String[] args) {
    ArrayList<UserInfo> list = new ArrayList();
    UserInfo user = new UserInfo(1, "张三", 1);
    list.add(user);
    list.add(new UserInfo(2, "李四", 2));
    list.add(new UserInfo(3, "王五", 3));
    list.add(new UserInfo(3, "赵六", 4));
    // 注意比较的是 地址值
    boolean anyMatch = list.stream().anyMatch(userInfo -> user == userInfo);
    System.out.println("AnyMatch结果:"   anyMatch);
    boolean allMatch = list.stream().allMatch(userInfo -> user == userInfo);
    System.out.println("AllMatch结果:"   allMatch);
}

Map用法

代码语言:javascript复制
public static void main(String[] args) {
    ArrayList<UserInfo> list = new ArrayList();
    list.add(new UserInfo(1, "张三", 1));
    list.add(new UserInfo(2, "李四", 2));
    list.add(new UserInfo(3, "王五", 3));
    list.add(new UserInfo(3, "赵六", 4));
    List<String> collect = list.stream()
            .distinct()
            .map(UserInfo::getName)
            .collect(Collectors.toList());
    System.out.println(collect);
}

Peek Stream打印日志

代码语言:javascript复制
public static void main(String[] args) {
    ArrayList<String> list = new ArrayList();
    list.add("A");
    list.add("A");
    list.add("B");
    list.add("A");
    List<String> collect = list.stream().distinct()
            .peek(e -> {
                System.out.println(e);
            })
            .collect(Collectors.toList());
}

Max、Min取值

Max min用法一致!

代码语言:javascript复制
    public static void main(String[] args) {
        ArrayList<UserInfo> list = new ArrayList();
        list.add(new UserInfo(1, "张三", 1));
        list.add(new UserInfo(2, "李四", 2));
        list.add(new UserInfo(3, "王五", 3));
        list.add(new UserInfo(3, "赵六", 4));
        Integer integer = list.stream()
                .distinct()
                .map(UserInfo::getId)
                // 如果List集合直接是int可直接用 .max(Integer::compare)
                .max(Integer::compare)
                .get();
        System.out.println(integer);
    }

Count统计元素个数

代码语言:javascript复制
public static void main(String[] args) {
    ArrayList<UserInfo> list = new ArrayList();
    list.add(new UserInfo(1, "张三", 1));
    list.add(new UserInfo(2, "李四", 2));
    list.add(new UserInfo(3, "王五", 3));
    list.add(new UserInfo(3, "赵六", 4));
    long count = list.stream().count();
    System.out.println(count);
}

特殊说明: 以上文章,均是我实际操作,写出来的笔记资料,不会盗用别人文章!烦请各位,请勿直接盗用!转载记得标注来源!

收心永久会员

打赏 收藏 海报 链接

0 人点赞