个人Stream常用操作
1、list转map
我们可以使用 Collectors.toMap()
方法来实现。
- Person对象类
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private String name; // 姓名
private int salary; // 薪资
private int age; // 年龄
private String sex; //性别
private String area; // 地区
}
- 重载方法一
@SpringBootTest
public class StreamDemoTest {
@Test
public void listToMapTest(){
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("Tom", 8900, 17,"male", "New York"));
personList.add(new Person("Jack", 7000,18 ,"male", "Washington"));
personList.add(new Person("Lily", 7800,20, "female", "Washington"));
personList.add(new Person("Anni", 8200,22, "female", "New York"));
personList.add(new Person("Owen", 9500,20, "male", "New York"));
personList.add(new Person("Alisa", 7900,18, "female", "New York"));
// 转换方式一
Map<String, Person> personMap = personList.stream()
.collect(Collectors.toMap(Person::getName, Function.identity()));
for (Map.Entry<String, Person> entry : personMap.entrySet()) {
System.out.println(entry);
}
}
我们可以将名称作为key,将对象本体作为value,结果如下
Tom=Person(name=Tom, salary=8900, age=17, sex=male, area=New York)
Owen=Person(name=Owen, salary=9500, age=20, sex=male, area=New York)
Anni=Person(name=Anni, salary=8200, age=22, sex=female, area=New York)
Alisa=Person(name=Alisa, salary=7900, age=18, sex=female, area=New York)
Jack=Person(name=Jack, salary=7000, age=18, sex=male, area=Washington)
Lily=Person(name=Lily, salary=7800, age=20, sex=female, area=Washington)
但是这样转换如果有相同的key的话会报错 java.lang.IllegalStateException: Duplicate key XXXX
。
这种情况下我们可以使用Collectors.toMap()
的重载方法来进行转换
- 重载方法二
当我们的map中可能存在重复key的时候,我们可以使用Collectors.toMap()
的重载方法,包含了一个mergeFunction
合并函数来处理重复key数据。
@Test
public void listToMapTest(){
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("Tom", 8900, 17,"男", "New York"));
personList.add(new Person("Tom", 7000,18 ,"女", "Washington"));
personList.add(new Person("Lily", 7800,20, "女", "Washington"));
personList.add(new Person("Anni", 8200,22, "女", "New York"));
personList.add(new Person("Tom", 9500,20, "男", "New York"));
personList.add(new Person("Lily", 7900,18, "男", "New York"));
// 转换为map时使用已存在数据
Map<String, Person> personMap = personList.stream()
.collect(Collectors.toMap(Person::getName, Function.identity(),
(exists,newest)-> exists ));
/* ==================== 打印结果 =================
Tom=Person(name=Tom, salary=8900, age=17, sex=male, area=New York)
Anni=Person(name=Anni, salary=8200, age=22, sex=female, area=New York)
Lily=Person(name=Lily, salary=7800, age=20, sex=female, area=Washington)
*/
// 转换为map时使用最新的数据替换旧数据
Map<String, Person> personMap1 = personList.stream()
.collect(Collectors.toMap(Person::getName, Function.identity(),
(exists,newest)-> newest ));
/* ==================== 打印结果 =================
Tom=Person(name=Tom, salary=9500, age=20, sex=male, area=New York)
Anni=Person(name=Anni, salary=8200, age=22, sex=female, area=New York)
Lily=Person(name=Lily, salary=7900, age=18, sex=female, area=New York)
*/
// 也可以进行判断后转换,当key重复时,如果最新数据性别为男则使用最新数据替换,否则使用已存在的旧数据
Map<String, Person> personMap2 = personList.stream()
.collect(Collectors.toMap(Person::getName, Function.identity(),
(exists,newest)-> newest.getSex().equals("男") ? newest : exists));
/* ==================== 打印结果 =================
Tom=Person(name=Tom, salary=9500, age=20, sex=男, area=New York)
Anni=Person(name=Anni, salary=8200, age=22, sex=女, area=New York)
Lily=Person(name=Lily, salary=7900, age=18, sex=男, area=New York)
*/
}
- 重载方法三
可以指定转换的map类型
TreeMap<String, Person> treeMap = personList.stream()
.collect(Collectors.toMap(Person::getName, Function.identity(),
(existing, replacement) -> existing, TreeMap::new));