5、java8的collector

1、collectorAPI

  1. averagingInt

  1. counting

  1. collectingAndThen

  1. groupingBy

  1. joining,把字符串链接

  1. MaxBy

  1. reducing

  1. toMap

  1. toList

2、案例

package java8;

import org.apache.commons.compress.utils.Lists;
import org.apache.commons.math3.fitting.leastsquares.EvaluationRmsChecker;
import org.apache.flink.calcite.shaded.com.google.common.collect.Maps;
import scala.App;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class CollectorExam6 {
    public static void main(String[] args) {
        Apple greenApple = new Apple("green", 150);
        Apple redApple = new Apple("red", 105);
        Apple greenApple1 = new Apple("green", 150);
        Apple redApple1 = new Apple("red", 105);
        List<Apple> appleList = Lists.newArrayList();
        appleList.add(greenApple);
        appleList.add(redApple);
        appleList.add(greenApple1);
        appleList.add(redApple1);
//        System.out.println(groupByNormal(appleList));
//        System.out.println(groupByFunctionalAndOptional(appleList));
        //groupingBy
//        System.out.println(groupByCollector(appleList));
        //averagingInt
        Optional.ofNullable(appleList.stream().collect(Collectors.averagingInt(apple -> {
            return apple.getWeight();
        }))).ifPresent(System.out::println);
        Optional.ofNullable(appleList.stream().collect(Collectors.averagingInt(Apple::getWeight)))
                .ifPresent(System.out::println);
        //counting
        Optional.ofNullable(appleList.stream().collect(Collectors.counting())).ifPresent(System.out::println);
        //collectingAndThen
        Optional.ofNullable(appleList.stream().collect(Collectors.collectingAndThen(Collectors.counting(), count -> {
            return count + "#";
        }))).ifPresent(System.out::println);
        //groupingBy
        Optional.ofNullable(appleList.stream().collect(Collectors.groupingBy(Apple::getColor, Collectors.averagingInt(Apple::getWeight))))
                .ifPresent(System.out::println);

        //joining,把字符串链接
        String joiningRes = appleList.stream().map(Apple::getColor).collect(Collectors.joining(","));
        Optional.ofNullable(joiningRes).ifPresent(System.out::println);

        //MaxBy
        Optional<Apple> maxByRes = appleList.stream().collect(Collectors.maxBy(Comparator.comparing(Apple::getWeight)));
        maxByRes.ifPresent(System.out::println);

        //reducing
        Integer weightSum = appleList.stream().map(Apple::getWeight).collect(Collectors.reducing(0, (a, b) -> {
            return a + b;
        }));
        System.out.println(weightSum);

        //toCollection
        Optional.ofNullable(appleList.stream().collect(Collectors.toCollection(HashSet::new)))
                .ifPresent(System.out::println);

        //toMap,必须写merge方法,否则如果key相同则报错
        Optional.ofNullable(appleList.stream().collect(Collectors.toMap(Apple::getColor, Apple::getWeight, (weight1, weight2) -> {
            return weight1 + weight2;
        })))
                .ifPresent(System.out::println);


    }

    private static Map<String, List<Apple>> groupByNormal(List<Apple> appleList) {
        Map<String, List<Apple>> listMap = Maps.newHashMap();
        appleList.forEach(apple -> {
            List<Apple> list = listMap.get(apple.getColor());
            if (list == null) {
                list = Lists.newArrayList();
                listMap.put(apple.getColor(), list);
            }
            list.add(apple);
        });

        return listMap;
    }

    //不用判空了
    private static Map<String, List<Apple>> groupByFunctionalAndOptional(List<Apple> appleList) {
        Map<String, List<Apple>> listMap = Maps.newHashMap();
        appleList.stream().forEach(apple -> {
            List<Apple> appleList1 = Optional.ofNullable(listMap.get(apple.getColor())).orElseGet(ArrayList::new);
            listMap.put(apple.getColor(), appleList1);
            appleList1.add(apple);
        });

        return listMap;
    }

    //Collectors的group by
    private static Map<String, List<Apple>> groupByCollector(List<Apple> appleList) {
        Map<String, List<Apple>> collectRes = appleList.stream().collect(Collectors.groupingBy(Apple::getColor));
        return collectRes;
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值