Java List 转map,分组,排序

目录

1.List 排序

1.排序字段无null值

2.排序字段有null值

3.多字段排序

2.List转Map

1.List转成Map,object>

2.List转成Map,string>

3.List>转成Map>,map

4.List>转成Map,object>

5.List>转成Map>,map

6.List>转成Map,string>

3.List分组

方法一:

方法二(不常用):


日常工作中经常需要用到对List集合的分组、排序、转Map等操作,每次都会忘记,然后去网上查,费时又费力,不如自己好好总结一下,提升工作效率和减少bug率。

1.List 排序

参考我写的另外一篇文章,写得更详细: Java8 对象List 排序_java8 list对象排序-CSDN博客 

1.排序字段无null值

Student User{
  private String name;
  private int age;
}

List list = Lists.newArrayList(new User("lao_wang", 21), new User("lao_wang", 22));

排序方式:

List nList = list.stream()
      .sorted(Comparator.comparing(User::getName))
      .collect(Collectors.toList());

2.排序字段有null值

Student User{
  private String name;
  private int age;
}

List list = Lists.newArrayList(new User(null, 23), new User("lao_wang", 21), new User("lao_wang", 22));

排序方式:

List nList = list.stream().sorted(
    Comparator.comparing(User::getName, Comparator.nullsFirst(String::compareTo)))
    .collect(Collectors.toList());
  • Comparator.nullsFirst: 排序字段为null的对象放在排序后的List最前面 
  • Comparator.nullsLast: 排序字段为null的对象放在排序后的List最后面

3.多字段排序

List按照多个字段排序,同步包含了处理null的功能。

List detailEntityList = list.stream().sorted(Comparator.comparing(User::getName,Comparator.nullsFirst(String::compareTo))
 .thenComparing(User::getAge,Comparator.nullsFirst(String::compareTo))).collect(Collectors.toList());

2.List转Map

实体类:

public class Person {
    private String name;
    private String address;

    public Person(String name, String address) {
        this.name = name;
        this.address = address;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

1.List转成Map

List list = new ArrayList<>();
Person person1 = new Person("熊大","森林第一个小屋");
Person person2 = new Person("熊二","森林第二个小屋");
Person person3 = new Person("光头强","森林第三个小屋");
Person person4 = new Person("熊大","森林第四个小屋");
list.add(person1);
list.add(person2);
list.add(person3);
list.add(person4);
Map map =  list.stream().collect(Collectors.toMap(Person::getName,each->each,(value1, value2) -> value1));
System.out.println(JSON.toJSONString(map));

结果:2个name为熊大,只保留了第一个,因为Map的key是不能重复的

{“光头强”:{“address”:“森林第三个小屋”,“name”:“光头强”},

“熊大”:{“address”:“森林第一个小屋”,“name”:“熊大”},

“熊二”:{“address”:“森林第二个小屋”,“name”:“熊二”}} 

2.List转成Map

List list = new ArrayList<>();
Person person1 = new Person("熊大","森林第一个小屋");
Person person2 = new Person("熊二","森林第二个小屋");
Person person3 = new Person("光头强","森林第三个小屋");
Person person4 = new Person("熊大","森林第四个小屋");
list.add(person1);
list.add(person2);
list.add(person3);
list.add(person4);
Map map =  list.stream().collect(Collectors.toMap(Person::getName,Person::getAddress,(value1, value2) -> value1));
System.out.println(JSON.toJSONString(map));

结果:

{“光头强”:“森林第三个小屋”,

“熊大”:“森林第一个小屋”,

“熊二”:“森林第二个小屋”} 

3.List>转成Map>

List> list = new ArrayList<>();
Person person1 = new Person("熊大","森林第一个小屋");
Person person2 = new Person("熊二","森林第二个小屋");
Person person3 = new Person("光头强","森林第三个小屋");
Map map1 = new HashMap<>();
map1.put("id","1");
map1.put("person",person1);
Map map2 = new HashMap<>();
map2.put("id","2");
map2.put("person",person2);
Map map3 = new HashMap<>();
map3.put("id","3");
map3.put("person",person3);
list.add(map1);
list.add(map2);
list.add(map3);
Map> map = list.stream().collect(Collectors.toMap(each->Objects.toString(each.get("id"),""),each->each,(key1,key2)->key1));
System.out.println(JSON.toJSONString(map));

 控制台打印日志:

{“1”:{“person”:{“address”:“森林第一个小屋”,“name”:“熊大”},“id”:“1”},

“2”:{“person”:{“address”:“森林第二个小屋”,“name”:“熊二”},“id”:“2”},

“3”:{“person”:{“address”:“森林第三个小屋”,“name”:“光头强”},“id”:“3”}}

4.List>转成Map

List> list = new ArrayList<>();
Person person1 = new Person("熊大","森林第一个小屋");
Person person2 = new Person("熊二","森林第二个小屋");
Person person3 = new Person("光头强","森林第三个小屋");
Map map1 = new HashMap<>();
map1.put("id","1");
map1.put("person",person1);
Map map2 = new HashMap<>();
map2.put("id","2");
map2.put("person",person2);
Map map3 = new HashMap<>();
map3.put("id","3");
map3.put("person",person3);
list.add(map1);
list.add(map2);
list.add(map3);
Map map = list.stream().collect(Collectors.toMap(each->Objects.toString(each.get("id"),""),each->each.get("person"),(key1,key2)->key1));
System.out.println(JSON.toJSONString(map));

结果:

{“1”:{“address”:“森林第一个小屋”,“name”:“熊大”},

“2”:{“address”:“森林第二个小屋”,“name”:“熊二”},

“3”:{“address”:“森林第三个小屋”,“name”:“光头强”}} 

5.List>转成Map>

List> list = new ArrayList<>();
Map map1 = new HashMap<>();
map1.put("id","1");
map1.put("name","熊大");
map1.put("address","森林第一个小屋");
Map map2 = new HashMap<>();
map2.put("id","2");
map2.put("name","熊二");
map2.put("address","森林第二个小屋");
Map map3 = new HashMap<>();
map3.put("id","3");
map3.put("name","光头强");
map3.put("address","森林第三个小屋");
list.add(map1);
list.add(map2);
list.add(map3);
Map> map = list.stream().collect(Collectors.toMap(each->each.get("id"),each->each,(key1,key2)->key1));
System.out.println(JSON.toJSONString(map));

结果:

{“1”:{“address”:“森林第一个小屋”,“name”:“熊大”,“id”:“1”},

“2”:{“address”:“森林第二个小屋”,“name”:“熊二”,“id”:“2”},

“3”:{“address”:“森林第三个小屋”,“name”:“光头强”,“id”:“3”}} 

6.List>转成Map

List> list = new ArrayList<>();
Map map1 = new HashMap<>();
map1.put("id","1");
map1.put("name","熊大");
map1.put("address","森林第一个小屋");
Map map2 = new HashMap<>();
map2.put("id","2");
map2.put("name","熊二");
map2.put("address","森林第二个小屋");
Map map3 = new HashMap<>();
map3.put("id","3");
map3.put("name","光头强");
map3.put("address","森林第三个小屋");
list.add(map1);
list.add(map2);
list.add(map3);
Map map = list.stream().collect(Collectors.toMap(each->each.get("id"),each->each.get("name"),(key1,key2)->key1));
System.out.println(JSON.toJSONString(map));

结果:

{“1”:“熊大”,“2”:“熊二”,“3”:“光头强”}

3.List分组

List 转成Map

方法一:
List list = new ArrayList<>();
Person person1 = new Person("熊大","森林第一个小屋");
Person person2 = new Person("熊二","森林第二个小屋");
Person person3 = new Person("光头强","森林第三个小屋");
Person person4 = new Person("熊大","森林第四个小屋");
list.add(person1);
list.add(person2);
list.add(person3);
list.add(person4);
Map> map = list.stream().collect(Collectors.groupingBy(Person::getName));
System.out.println(JSON.toJSONString(map));

 控制台打印日志:

{“光头强”:[{“address”:“森林第三个小屋”,“name”:“光头强”}],“熊大”:[{“address”:“森林第一个小屋”,“name”:“熊大”},{“address”:“森林第四个小屋”,“name”:“熊大”}],“熊二”:[{“address”:“森林第二个小屋”,“name”:“熊二”}]}

方法二(不常用):
List list = new ArrayList<>();
Person person1 = new Person("熊大","森林第一个小屋");
Person person2 = new Person("熊二","森林第二个小屋");
Person person3 = new Person("光头强","森林第三个小屋");
Person person4 = new Person("熊大","森林第四个小屋");
list.add(person1);
list.add(person2);
list.add(person3);
list.add(person4);
Map> map =  list.stream().collect(Collectors.toMap(Person::getName,each->Collections.singletonList(each),(value1, value2) -> {
        List union = new ArrayList<>(value1);
        union.addAll(value2);
        return union;
    }));
System.out.println(JSON.toJSONString(map));

 结果:

{“光头强”:[{“address”:“森林第三个小屋”,“name”:“光头强”}],“熊大”:[{“address”:“森林第一个小屋”,“name”:“熊大”},{“address”:“森林第四个小屋”,“name”:“熊大”}],“熊二”:[{“address”:“森林第二个小屋”,“name”:“熊二”}]}

你可能感兴趣的:(java,list,开发语言)