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

函数式编程,Lambda表达式,Stream流高级用法,Optional空值处理,函数式接口编写,lambda方法引用,并行流实现

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

函数式编程,Lambda表达式,Stream流高级用法,Optional空值处理,函数式接口编写,lambda方法引用,并行流实现

一、函数式编程思想:主要关注对数据进行什么操作,易于并发编程

二、Lambda表达式:是函数式编程的体现。

例:替换匿名内部类

    //匿名内部类
    new Thread(new Runnable(){
        @Override
        public void run(){
            System.out.println("test");
        }
    }).start();
    //函数式编程 -> 只关注方法实现本省,去掉冗余代码
    new Thread(()->{
        System.out.println("test");
    }).start();

省略规则:

参数可省略;方法体中只有一句代码时大括号return和唯一一句代码的分号可以省略;方法只有一个参数时小括号可以省略

三、Stream流:java8的Stream使用的是函数式编程模式,被用来对集合或数组进行链状流式操作。

创建流:
    public class Author {
        private String id;
        private String name;
        private int age;
        private String intro;
        private List book;
        public Author(List book) {this.book = book;}
        public void setBook(List book) {this.book = book;}
        public List getBook() {return book;}
        public String getId() {return id;}
        public void setId(String id) {this.id = id;}
        public String getName() {return name;}
        public void setName(String name) {this.name = name;}
        public int getAge() {return age;}
        public void setAge(int age) {this.age = age;}
        public String getIntro() {return intro;}
        public void setIntro(String intro) {this.intro = intro;}
    }
    public class Book {
        private String id;
        private String name;
        private String category;
        private String score;
        private String intro;
        public String getId() {return id;}
        public void setId(String id) {this.id = id;}
        public String getName() {return name;}
        public void setName(String name) {this.name = name;}
        public String getCategory() {return category;}
        public void setCategory(String category) {this.category = category;}
        public String getScore() {return score;}
        public void setScore(String score) {this.score = score;}
        public String getIntro() {return intro;}
        public void setIntro(String intro) {this.intro = intro;}
    }
        
        //单列集合
        ArrayList authors = new ArrayList<>();
        Stream stream = authors.stream();

        //数组
        Integer[] arr = {1,2,3,4,5};
        Stream stream1 = Arrays.stream(arr);

        //双列集合
        Map map = new HashMap<>();
        map.put("1",1);
        map.put("2",2);
        map.put("3",3);
        Stream> stream2 = map.entrySet().stream();
中间操作:
        
        authors.stream()
                .distinct()
                .filter(bean->bean.getAge()<18)
                .forEach(bean-> System.out.println(bean.getName()));
        authors.stream()
                .map(bean->bean.getAge()+10)
                .forEach(x-> System.out.println(x));
        authors.stream()
                .distinct()
                .forEach(bean-> System.out.println(bean.getName()));
        authors.stream()
                .sorted(Comparator.comparing(bean->bean.getAge()))
                .forEach(bean-> System.out.println(bean.getAge()));
        authors.stream()
                .distinct()
                .sorted(Comparator.comparing(bean->bean.getAge()))
                .limit(2)
                .forEach(bean->bean.getName());
        authors.stream()
                .distinct()
                .sorted(Comparator.comparing(bean->bean.getAge()))
                .skip(1)
                .forEach(bean->bean.getName());
        authors.stream()
                .flatMap(bean->bean.getBook().stream())
                .forEach(book->book.getName());
        authors.stream()//打印数据所有分类,要求对分类进行去重,按照“,”分割开
                .flatMap(bean->bean.getBook().stream())
                .distinct()
                .flatMap(book->Arrays.stream(book.getCategory().split(",")))  //把数字转换成流
                .distinct();
终结操作:
        authors.stream()
                .map(bean->bean.getName())
                .distinct()
                .forEach(name-> System.out.println(name));
        authors.stream()
                .flatMap(bean->bean.getBook().stream())
                .distinct()
                .count();
        Optional max = authors.stream()
                .flatMap(bean->bean.getBook().stream())
                .map(x->x.getScore())
                .max((score1,score2) -> score1 - score2);
        authors.stream()
                .map(bean->bean.getName())
                .collect(Collectors.toList());
        authors.stream()
                .flatMap(bean->bean.getBook().stream())
                .collect(Collectors.toSet());
        authors.stream()
                .collect(Collectors.toMap(new Function() {
                    @Override
                    public String apply(Author author) {
                        return author.getName();
                    }
                }, new Function>() {
                    @Override
                    public List apply(Author author) {
                        return author.getBook();
                    }
                }));
        Map> collect = authors.stream()
                .distinct()
                .collect(Collectors.toMap(author -> author.getName(), author -> author.getBook()));
        //判断是否有年龄在28以上的作家
        authors.stream()
                .anyMatch(bean->bean.getAge()>28);
        //判断是否所有的作家都是成年
        authors.stream()
                .allMatch(bean->bean.getAge() >= 18);
        //判断所有的作家都没有超过100
        authors.stream()
                .noneMatch(bean->bean.getAge()>100);
        //获取任意一个大于18的作家,如果存在就输出
        authors.stream()
                .filter(bean->bean.getAge()>18)
                .findAny().ifPresent(bean-> System.out.println(bean.getName())); //如果不为null,则输出名字,为null则不输出
        //获取一个年龄最小的作家,并输出他的名字
        authors.stream()
                .sorted(Comparator.comparing(bean->bean.getAge()))
                .findFirst().ifPresent(bean->bean.getName());

        //使用reduce求所有作者年龄的和
        authors.stream()
                .map(bean->bean.getAge())
                .reduce(0, new BinaryOperator() {
                    @Override
                    public Integer apply(Integer result, Integer element) {
                        return result+element;
                    }
                });
        authors.stream()
                .map(bean->bean.getAge())
                .reduce(0, (result,element) -> result + element);
        //使用reduce求所有作者中年龄最大的
        authors.stream()
                .map(bean->bean.getAge())
                .reduce(Integer.MIN_VALUE,(result,element) -> result< element?element:result);
        //使用reduce求所有作者中年龄最小的
        authors.stream()
                .map(bean->bean.getAge())
                .reduce(new BinaryOperator() {
                    @Override
                    public Integer apply(Integer result, Integer element) {
                        return result > element ? element : result;
                    }
                }).ifPresent(age-> System.out.println(age));
        authors.stream()
                .map(Author::getAge)
                .reduce((result, element) -> result > element ? element : result);
Optional空值处理:
 
函数式接口 
 
方法引用 
 
高级用法 
 
并行流 
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/839071.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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