分组后返回有序的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个参数
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()))