Stream中Collectors.groupingBy toMap mapping操作实例

groupingBy用于分组,toMap用于list转map

测试代码:

1.建一个实体类,测试中用

package com.xhx.java;

/**
 * xuhaixing
 * 2018/7/20 21:43
 **/
public class Student {
    private String name;
    private String sex;
    private double money;

    public Student(String name, String sex, double money) {
        this.name = name;
        this.sex = sex;
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", money=" + money +
                '}';
    }
}

 

2.具体测试代码及输出结果

package com.xhx.java;

import org.junit.Test;

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

/**
 * xuhaixing
 * 2018/7/20 21:44
 **/
public class TestCollectors {

    /**
     * Collectors.groupingBy 分组1
     */
    @Test
    public void testGrouping() {
        List items = Arrays.asList("apple", "banana", "apple", "orange", "banana");
        Collector> objectMapCollector = Collectors.groupingBy(Function.identity(), Collectors.counting());
        Map collect = items.stream().collect(objectMapCollector);
        System.out.println(collect);
        //{orange=1, banana=2, apple=2}
    }

    /**
     * Collectors.groupingBy 分组2
     */
    @Test
    public void testGrouping2() {
        List students = Arrays.asList(new Student("apple", "男", 10.0),
                new Student("banana", "男", 10.0),
                new Student("orange", "男", 20.0),
                new Student("pipe", "女", 40.0),
                new Student("pinck", "女", 80.0)
        );

        //根据sex分组
        Map> collect = students.stream().collect(Collectors.groupingBy(Student::getSex));
        System.out.println(collect);
        //{
        // 女=[Student{name='pipe', sex='女', money=40.0}, Student{name='pinck', sex='女', money=80.0}],
        // 男=[Student{name='apple', sex='男', money=10.0}, Student{name='banana', sex='男', money=10.0}, Student{name='orange', sex='男', money=20.0}]
        // }

        //根据sex分组,然后对money求和
        Map collect1 = students.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.summingDouble(Student::getMoney)));
        System.out.println(collect1);
        //{女=120.0, 男=40.0}
    }

    /**
     * list、set
     */
    @Test
    public void testGrouping3() {
        List students = Arrays.asList(new Student("apple", "男", 10.0),
                new Student("banana", "男", 10.0),
                new Student("orange", "男", 20.0),
                new Student("pipe", "女", 40.0),
                new Student("pinck", "女", 80.0)
        );
        Map> collect1 = students.stream().collect(Collectors.groupingBy(Student::getSex));
        System.out.println(collect1);
        //{
        // 女=[Student{name='pipe', sex='女', money=40.0}, Student{name='pinck', sex='女', money=80.0}],
        // 男=[Student{name='apple', sex='男', money=10.0}, Student{name='banana', sex='男', money=10.0}, Student{name='orange', sex='男', money=20.0}]
        // }

        Map> collect2 = students.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.mapping(Student::getMoney, Collectors.toList())));
        System.out.println(collect2);
        //{女=[40.0, 80.0], 男=[10.0, 10.0, 20.0]}

        Map> collect3 = students.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.mapping(Student::getMoney, Collectors.toSet())));
        System.out.println(collect3);
        //{女=[40.0, 80.0], 男=[10.0, 20.0]}
    }

    /**
     * toMap 转成map
     */
    @Test
    public void tesToMap(){
        List students = Arrays.asList(new Student("apple", "男", 10.0),
                new Student("banana", "男", 10.0),
                new Student("orange", "男", 20.0),
                new Student("pipe", "女", 40.0),
                new Student("pinck", "女", 80.0)
        );


        Map collect = students.stream().collect(Collectors.toMap(Student::getName, Student::getMoney));
        System.out.println(collect);
        //{orange=20.0, banana=10.0, apple=10.0, pinck=80.0, pipe=40.0}
    }

    /**
     * toMap 转成map  key重复
     */
    @Test
    public void tesToMap2(){
        List students = Arrays.asList(new Student("apple", "男", 10.0),
                new Student("banana", "男", 10.0),
                new Student("orange", "男", 20.0),
                new Student("pipe", "女", 40.0),
                new Student("pinck", "女", 80.0)
        );
        /*
        java.lang.IllegalStateException: Duplicate key apple
        at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
        at java.util.HashMap.merge(HashMap.java:1254)
        at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
         */
//        Map collect2 = students.stream().collect(Collectors.toMap(Student::getMoney, Student::getName));
//        System.out.println(collect2);

        Map collect2 = students.stream().collect(Collectors.toMap(Student::getMoney, Student::getName,(oldValue,newValue)->newValue));
        System.out.println(collect2);
        //{80.0=pinck, 40.0=pipe, 20.0=orange, 10.0=banana}  key重复用新值

    }

    /**
     * toMap 转成map 排序
     */
    @Test
    public void tesToMap3() {
        List students = Arrays.asList(new Student("apple", "男", 10.0),
                new Student("banana", "男", 10.0),
                new Student("orange", "男", 20.0),
                new Student("pipe", "女", 40.0),
                new Student("pinck", "女", 80.0)
        );

        LinkedHashMap collect = students.stream().sorted(Comparator.comparing(Student::getMoney).reversed())
                .collect(Collectors.toMap(Student::getName, Student::getMoney, (oldValue, newValue) -> newValue, LinkedHashMap::new));
        System.out.println(collect);
        //{pinck=80.0, pipe=40.0, orange=20.0, apple=10.0, banana=10.0}
    }
}

需要注意list转set,set中的值不允许有重复,所以只有一个,,list转map,map的key不能重复,所以自己设置选用老的还是新的,还一点需要注意,list转map时key可以为null,value不可以为null,否则会报空指针异常

实时内容请关注微信公众号,公众号与博客同时更新:程序员星星

Stream中Collectors.groupingBy toMap mapping操作实例_第1张图片

 

你可能感兴趣的:(java)