编程语言
首页 > 编程语言> > Java8新特性-Stream流

Java8新特性-Stream流

作者:互联网

01 、概述

02、 Stream流的作用

  1. 对于集合迭代的增强处理
  2. 可以对于集合数组进行更高效的聚合操作,比如: 分组、过滤、排序,元素的追加等。
  3. 解决了的传统开发过程中,jdk对集合或者数组API不足的问题,因为在早期的API的开发过程中,对集合或者Map的操作其实比较单一和缺乏。在jdk1.8之后就参考了很多语言的一些对数组和集合操作的API。 比如JS的数组的reduce、map、sort、filter 等等

03、 Stream流的作用对象

04、 Stream的案例分析

public class Category {
    
    // 主键
    private Long id;
    // 分类标题
    private String title;
    // 子标题
    private String subtitle;
    // 父id
    private Long pid;
    // 排序
    private Integer sort;
    // 价格
    private Float price;
}

public class StreamDemo01 {

    public static void main(String[] args) {
        // 1. 创建一个List集合
        List<Category> categoryList = new ArrayList<>();
        // List<Category> categoryList2 = Collections.emptyList();

        categoryList.add(new Category(1L,"Java","Java",0L,1,1.2f));
        categoryList.add(new Category(2L,"PHP","PHP",0L,2,2.2f));
        categoryList.add(new Category(3L,"JavaScript","JavaScript",0L,3,2.2f));
        categoryList.add(new Category(4L,"Python","Python",0L,10,2.2f));
        categoryList.add(new Category(5L,"Go","Go",0L,8,2.2f));
        categoryList.add(new Category(6L,"Ruby","Ruby",0L,4,2.2f));
    }

    public void StreamSort(List<Category> categoryList){
        //  stream 完成排序
        categoryList.stream().sorted(new Comparator<Category>() {
            @Override
            public int compare(Category o1, Category o2) {
                return o1.getSort()-o2.getSort();
            }
        }).collect(Collectors.toList());

        //System.out.println(categoryList);
        categoryList.forEach(System.out::println);
    }

    /**
     * 过滤 是指 将满足过滤条件的信息,选出来
     * @param categoryList
     */
    public void StreamFile(List<Category> categoryList){
        List<Category> categories = categoryList.stream()
                .filter(cate -> cate.getId().equals(2L)).collect(Collectors.toList());

        categoryList.forEach(System.out::println);
    }


    public void StreamMap(List<Category> categoryList){
        //map改变集合中每个元素的信息
        categoryList.stream().map(cate->{
            cate.setSubtitle(cate.getSubtitle()+"_yykk");
            return cate;
        }).collect(Collectors.toList());
        categoryList.forEach(System.out::println);
    }

    /**
     * 统计集合中元素的数量
     * @param categoryList
     */
    public void StreamCount(List<Category> categoryList){
        long count = categoryList.stream().count();
        System.out.println(count);
    }

    /**
     * 遍历集合
     * @param categoryList
     */
    public void StreamForEach(List<Category> categoryList){
        categoryList.stream().forEach(category -> System.out.println(category));
    }

    /**
     * distinct可以去重元素,如果集合是对象,要distinct,需要对象返回相同的hashcode和 equals
     * @param categoryList
     */
    public void StreamDistinct(List<Category> categoryList){
        categoryList.stream().distinct().collect(Collectors.toList());
        categoryList.forEach(System.out::println);
    }

    /**
     * 求出最大sort的信息
     * @param categoryList
     */
    public void StreamMax(List<Category> categoryList){
        Optional<Category> optionalCategory = categoryList.stream().max((o1,o2)-> o1.getSort()-o2.getSort());
        System.out.println(optionalCategory);
    }

    /**
     * 求集合中某一对象元素属性的和
     * @param categoryList
     */
    public void StreamReduce(List<Category> categoryList){
        // 对集合中的元素的价格求总和
        Float reduce = categoryList.stream().map(res -> {
            return res.getPrice();
        }).reduce(0f, (c1, c2) -> c1 + c2);
        System.out.println(reduce);
    }
}
public class User {

    private Long id;
    private String username;
    private String password;
}
public class StreamDemo02 {
    public static void main(String[] args) {
        List<User> userList = new ArrayList<>();
        userList.add(new User(1L,"yykk1","123456"));
        userList.add(new User(2L,"yykk2","123456"));
        userList.add(new User(3L,"yykk3","123456"));

        List<User> collect = userList.stream().map(user -> {
            user.setPassword("");
            return user;
        }).collect(Collectors.toList());

        collect.forEach(System.out::println);
    }
}

标签:stream,Stream,void,List,特性,public,new,categoryList,Java8
来源: https://blog.csdn.net/weixin_44107920/article/details/120671938