栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java8新特性-Stream流

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java8新特性-Stream流

01 、概述
  • Stream 是JDK1.8的新特性,可以结合lambada表达式结合使用 可以提升开发的效率和性能。
02、 Stream流的作用
  1. 对于集合迭代的增强处理
  2. 可以对于集合数组进行更高效的聚合操作,比如: 分组、过滤、排序,元素的追加等。
  3. 解决了的传统开发过程中,jdk对集合或者数组API不足的问题,因为在早期的API的开发过程中,对集合或者Map的操作其实比较单一和缺乏。在jdk1.8之后就参考了很多语言的一些对数组和集合操作的API。 比如JS的数组的reduce、map、sort、filter 等等
03、 Stream流的作用对象
  • 集合(List、Set)===(数组、map)
04、 Stream的案例分析
public class Category {
    
    // 主键
    private Long id;
    // 分类标题
    private String title;
    // 子标题
    private String subtitle;
    // 父id
    private Long pid;
    // 排序
    private Integer sort;
    // 价格
    private Float price;
}
  • stream的api
public class StreamDemo01 {

    public static void main(String[] args) {
        // 1. 创建一个List集合
        List categoryList = new ArrayList<>();
        // List 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 categoryList){
        //  stream 完成排序
        categoryList.stream().sorted(new Comparator() {
            @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);
    }

    
    public void StreamFile(List categoryList){
        List categories = categoryList.stream()
                .filter(cate -> cate.getId().equals(2L)).collect(Collectors.toList());

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


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

    
    public void StreamCount(List categoryList){
        long count = categoryList.stream().count();
        System.out.println(count);
    }

    
    public void StreamForEach(List categoryList){
        categoryList.stream().forEach(category -> System.out.println(category));
    }

    
    public void StreamDistinct(List categoryList){
        categoryList.stream().distinct().collect(Collectors.toList());
        categoryList.forEach(System.out::println);
    }

    
    public void StreamMax(List categoryList){
        Optional optionalCategory = categoryList.stream().max((o1,o2)-> o1.getSort()-o2.getSort());
        System.out.println(optionalCategory);
    }

    
    public void StreamReduce(List categoryList){
        // 对集合中的元素的价格求总和
        Float reduce = categoryList.stream().map(res -> {
            return res.getPrice();
        }).reduce(0f, (c1, c2) -> c1 + c2);
        System.out.println(reduce);
    }
}
  • stream的应用 —— 过滤密码
public class User {

    private Long id;
    private String username;
    private String password;
}
public class StreamDemo02 {
    public static void main(String[] args) {
        List 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 collect = userList.stream().map(user -> {
            user.setPassword("");
            return user;
        }).collect(Collectors.toList());

        collect.forEach(System.out::println);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/325499.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号