java8自带常用的函数式接口 和 stream api流式编程

java8自带常用的函数式接口

Function<T,R> 	R apply(T t)  			接收一个T类型的参数,返回一个R类型的结果
Predicate<T> 	boolean test(T t)  		接收一个T类型的参数,返回一个boolean类型的结果
Consumer<T> 	void accept(T t)    	接收一个T类型的参数,不返回值
Supplier<T>					    		不接受参数,返回一个T类型的结果

准备数据

 //计算机俱乐部
    private static List<Student> computerClub = Arrays.asList(
            new Student("2015134001", "小明", 15, "1501"),
            new Student("2015134003", "小王", 14, "1503"),
            new Student("2015134006", "小张", 15, "1501"),
            new Student("2015134008", "小梁", 17, "1505")
    );
    //篮球俱乐部
    private static List<Student> basketballClub = Arrays.asList(
            new Student("2015134012", "小c", 13, "1503"),
            new Student("2015134013", "小s", 14, "1503"),
            new Student("2015134015", "小d", 15, "1504"),
            new Student("2015134018", "小y", 16, "1505")
    );
    //乒乓球俱乐部
    private static List<Student> pingpongClub = Arrays.asList(
            new Student("2015134022", "小u", 16, "1502"),
            new Student("2015134021", "小i", 14, "1502"),
            new Student("2015134026", "小m", 17, "1504"),
            new Student("2015134027", "小n", 16, "1504")
    );

    private static List<List<Student>> allClubStu = new ArrayList<>();
    allClubStu.add(computerClub);
    allClubStu.add(basketballClub);
    allClubStu.add(pingpongClub);

常用的stream三种创建方式

集合 Collection.stream()
静态方法 Stream.of
数组 Arrays.stream

//1.集合
        Stream<Student> stream = basketballClub.stream();
        //2.静态方法
        Stream<String> stream2 = Stream.of("a", "b", "c");
        //3.数组
        String[] arr = {"a","b","c"};
        Stream<String> stream3 = Arrays.stream(arr);

Stream的终止操作

  • foreach(Consumer c) 遍历操作
  • collect(Collector) 将流转化为其他形式
  • max(Comparator)返回流中最大值
  • min(Comparator) 返回流中最小值
  • count 返回流中元素综述

Collectors 具体方法

  • toList List 把流中元素收集到List
  • toSet Set 把流中元素收集到Set
  • toCollection Coolection 把流中元素收集到Collection中
  • groupingBy Map<K,List> 根据K属性对流进行分组
  • partitioningBy Map<boolean, List> 根据boolean值进行分组

Stream的中间操作

  1. filter(Predicate) 筛选流中某些元素
//筛选1501班的学生
computerClub.stream().filter(e -> e.getClassNum().equals("1501")).forEach(System.out::println);
//筛选年龄大于15的学生
List<Student> collect = computerClub.stream().filter(e -> e.getAge() > 15).collect(Collectors.toList());
  1. map(Function f) 接收流中元素,并且将其映射成为新元素,例如从student对象中取name属性
//篮球俱乐部所有成员名 + 暂时住上商标^_^,并且获取所有队员名
        List<String> collect1 = basketballClub.stream()
                .map(e -> e.getName() + "^_^")
                .collect(Collectors.toList());
        collect1.forEach(System.out::println);
        //小c^_^^_^
        //小s^_^^_^
        //小d^_^^_^
        //小y^_^^_^
  1. flatMap(Function f) 将所有流中的元素并到一起连接成一个流
		//获取年龄大于15的所有俱乐部成员
        List<Student> collect2 = Stream.of(basketballClub, computerClub, pingpongClub)
                .flatMap(e -> e.stream().filter(s -> s.getAge() > 15))
                .collect(Collectors.toList());
        collect2.forEach(System.out::println);

        //用双层list获取所有年龄大于15的俱乐部成员
        List<Student> collect3 = allClubStu.stream()
                .flatMap(e -> e.stream().filter(s -> s.getAge() > 15))
                .collect(Collectors.toList());
        collect3.forEach(System.out::println);

注:flatMap在于方法体里面,将遍历的元素依次转换为流,这样就有了多个流,然后每个流中进行筛选,最后将每个流中剩下的元素并在一起,形成一个流

  1. peek(Consumer c) 获取流中元素,操作流中元素,与foreach不同的是不会截断流,可继续操作流
		//篮球俱乐部所有成员名 + 赞助商商标^_^,并且获取所有队员详细内容
        List<Student> collect = basketballClub.stream()
                .peek(e -> e.setName(e.getName() + "^_^"))
                .collect(Collectors.toList());
        collect.forEach(System.out::println);
        //Student{idNum='2015134012', name='小c^_^', age=13, classNum='1503'}
        //Student{idNum='2015134013', name='小s^_^', age=14, classNum='1503'}
        //Student{idNum='2015134015', name='小d^_^', age=15, classNum='1504'}
        //Student{idNum='2015134018', name='小y^_^', age=16, classNum='1505'}
  1. distinct() 通过流所生成元素的equals和hashCode去重
  2. limit(long val) 截断流,取流中前val个元素
  3. sorted(Comparator) 产生一个新流,按照比较器规则排序
  4. sorted() 产生一个新流,按照自然顺序排序
 		List<String> list = Arrays.asList("b","b","c","a");
        list.forEach(System.out::print); //bbca
        List<String> collect = list.stream().distinct().sorted().collect(Collectors.toList());
        collect.forEach(System.out::print);//abc
        //获取list中排序后的top2 即截断取前两个
        List<String> collect1 = list.stream().distinct().sorted().limit(2).collect(Collectors.toList());
        collect1.forEach(System.out::print);//ab

匹配

booelan allMatch(Predicate) 都符合
.boolean anyMatch(Predicate) 任一元素符合
boolean noneMatch(Predicate) 都不符合

寻找元素

findFirst——返回第一个元素
findAny——返回当前流中的任意元素

 		Optional<Student> first = basketballClub.stream().findFirst();
        if (first.isPresent()) {
            Student student = first.get();
            System.out.println(student);
        }

        Optional<Student> any = basketballClub.stream().findAny();
        if (any.isPresent()) {
            Student student2 = any.get();
            System.out.println(student2);
        }
        Optional<Student> any1 = basketballClub.stream().parallel().findAny();
        System.out.println(any1);

计数和极值

count——返回流中元素的总个数
max——返回流中最大值
min——返回流中最小值

		long count = basketballClub.stream().count();
        Optional<Student> max = basketballClub.stream().max(Comparator.comparing(Student::getAge));
        if (max.isPresent()) {
            Student student = max.get();
        }
        Optional<Student> min = basketballClub.stream().min(Comparator.comparingInt(Student::getAge));
        if (min.isPresent()) {
            Student student = min.get();
        }

转载链接:https://www.cnblogs.com/haixiang/p/14291626.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值