java8中使用groupingBy分组返回有序的Map

分组后返回有序的Map:
使用 (Collectors.groupingBy(User::getType , LinkedHashMap::new, Collectors.toList()))

LinkedHashMap取键值对时,是按照放入的顺序

LinkedHashMap<String, List<User>> groupMap2 = list.stream().collect(Collectors.groupingBy(User::getType, LinkedHashMap::new, Collectors.toList()));

groupby方法有几个重载方法,上面使用的这个方法有3个参数

  1. 第一个参数:分组按照什么进行分类
  2. 第二个参数:分组结果最后用什么容器保存并返回,这里指定为LinkedHashMap
  3. 第三个参数:分类后,对应的分类的结果如何收集
package com.test;

public class User {
    private Integer id;
    private String name;
    private String type;

    public User() {
    }

    public User(Integer id, String name, String type) {
        this.id = id;
        this.name = name;
        this.type = type;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", type='" + type + '\'' +
                '}';
    }
}
package com.test;

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

public class MainUser {
    /**
     * 构造一个集合
     */
    public static List<User> getUserList() {
        User user1 = new User(1, "张", "A");
        User user2 = new User(2, "李", "BB");
        User user3 = new User(3, "王", "BB");
        User user4 = new User(4, "马", "CCC");
        User user5 = new User(5, "赵", "CCC");
        User user6 = new User(6, "钱", "CCC");

        List<User> list = new ArrayList<User>();
        list.add(user2);
        list.add(user4);
        list.add(user3);
        list.add(user1);
        list.add(user5);
        list.add(user6);

        return list;
    }

    /**
     * 打印出所有的 key 及 对应list中的元素
     */
    private static void print(Map<String, List<User>> groupMap) {
        groupMap.forEach((key, list) -> {
            System.out.println(key);
            list.forEach(System.out::println);
        });
    }

    public static void main(String[] args) {
        List<User> list = getUserList();
        //根据 id 升序
        list = list.stream().sorted(Comparator.comparing(User::getId)).collect(Collectors.toList());
        list.forEach(System.out::println);
        System.out.println();

        //(1)这样分组,key 是无序的
        Map<String, List<User>> groupMap1 = list.stream().collect(Collectors.groupingBy(User::getType));
        print(groupMap1);

        System.out.println();

        //(2)这样分组,key 还是原来的顺序
        Map<String, List<User>> groupMap2 = list.stream().collect(Collectors.groupingBy(User::getType, LinkedHashMap::new, Collectors.toList()));
// LinkedHashMap> groupMap2 = list.stream().collect(Collectors.groupingBy(User::getType, LinkedHashMap::new, Collectors.toList()));
        print(groupMap2);
    }
}

输出结果:

User{id=1, name='张', type='A'}
User{id=2, name='李', type='BB'}
User{id=3, name='王', type='BB'}
User{id=4, name='马', type='CCC'}
User{id=5, name='赵', type='CCC'}
User{id=6, name='钱', type='CCC'}

BB
User{id=2, name='李', type='BB'}
User{id=3, name='王', type='BB'}
A
User{id=1, name='张', type='A'}
CCC
User{id=4, name='马', type='CCC'}
User{id=5, name='赵', type='CCC'}
User{id=6, name='钱', type='CCC'}

A
User{id=1, name='张', type='A'}
BB
User{id=2, name='李', type='BB'}
User{id=3, name='王', type='BB'}
CCC
User{id=4, name='马', type='CCC'}
User{id=5, name='赵', type='CCC'}
User{id=6, name='钱', type='CCC'}

主要就是
(Collectors.groupingBy(User::getType))
修改为
(Collectors.groupingBy(User::getType , LinkedHashMap::new, Collectors.toList()))

你可能感兴趣的:(一些问题,java,stream,排序,分组)