1.创建Person类
package com.xkzhangsan.normal.collectors; public class Person { private Integer id;
private String name;
private Integer score;
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 Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
} @Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", score=" + score + "]";
} }
2.创建测试类ListToMap
package com.xkzhangsan.normal.collectors; import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors; public class ListToMap { public static void main(String[] args) {
//创建list
List<Person> personList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Person p = new Person();
p.setId(i);
p.setName("p"+i);
p.setScore(i*10);
personList.add(p);
} //添加和id=8相同对象score值不同
Person p = new Person();
p.setId(8);
p.setName("p"+8);
p.setScore(88);
personList.add(p); System.out.println("list:========================");
personList.stream().forEach(System.out::println); //转换为HashMap
Map<Integer, Person> map = personList.stream().collect(Collectors.toMap(Person::getId, d->d, (oldValue, newValue)->newValue));
System.out.println("hashMap:========================");
map.entrySet().stream().forEach(System.out::println); //转换为TreeMap
Map<Integer, Person> treeMap = personList.stream().collect(Collectors.toMap(Person::getId, d->d, (oldValue, newValue)->newValue, TreeMap::new));
System.out.println("treeMap:========================");
treeMap.entrySet().stream().forEach(System.out::println);
} }
3.测试结果
list:========================
Person [id=0, name=p0, score=0]
Person [id=1, name=p1, score=10]
Person [id=2, name=p2, score=20]
Person [id=3, name=p3, score=30]
Person [id=4, name=p4, score=40]
Person [id=5, name=p5, score=50]
Person [id=6, name=p6, score=60]
Person [id=7, name=p7, score=70]
Person [id=8, name=p8, score=80]
Person [id=9, name=p9, score=90]
Person [id=8, name=p8, score=88]
hashMap:========================
0=Person [id=0, name=p0, score=0]
1=Person [id=1, name=p1, score=10]
2=Person [id=2, name=p2, score=20]
3=Person [id=3, name=p3, score=30]
4=Person [id=4, name=p4, score=40]
5=Person [id=5, name=p5, score=50]
6=Person [id=6, name=p6, score=60]
7=Person [id=7, name=p7, score=70]
8=Person [id=8, name=p8, score=88]
9=Person [id=9, name=p9, score=90]
treeMap:========================
0=Person [id=0, name=p0, score=0]
1=Person [id=1, name=p1, score=10]
2=Person [id=2, name=p2, score=20]
3=Person [id=3, name=p3, score=30]
4=Person [id=4, name=p4, score=40]
5=Person [id=5, name=p5, score=50]
6=Person [id=6, name=p6, score=60]
7=Person [id=7, name=p7, score=70]
8=Person [id=8, name=p8, score=88]
9=Person [id=9, name=p9, score=90]
4.注意事项
(1)list转map要注意重复对象,map转换方法要选择带mergeFunction参数的方法,如果key值重复,做合并处理,不然会抛异常!可以做到去重效果。
比如上面故意添加和id=8相同对象score为88,值不同。在map转换方法mergeFunction 为(oldValue, newValue)->newValue 使用新对象替换已有老对象,可以看到转换后id8的对象score变为88。
(2)list转map默认转换为HashMap,可以选择带mapSupplier参数的方法,选择要转换为的map类型。
比如上面TreeMap::new,选择转换为TreeMap。