邢台网站建设58,北京网站推广排名,最牛的科技网站建设,中小企业网站制作费用在Java中#xff0c;使用Stream API可以轻松地对集合进行操作#xff0c;包括将List转换为Map或LinkedHashMap。本篇博客将演示如何利用Java Stream实现这两种转换#xff0c;同时假设List中的元素是User对象。 1. 数据准备
ListUser list new ArrayList(…在Java中使用Stream API可以轻松地对集合进行操作包括将List转换为Map或LinkedHashMap。本篇博客将演示如何利用Java Stream实现这两种转换同时假设List中的元素是User对象。 1. 数据准备
ListUser list new ArrayList();
list.add(new User(1, 张三, 我是张三01));
list.add(new User(2, 张三, 我是张三02));
list.add(new User(3, 李四, 我是李四01));
list.add(new User(4, 李四, 我是李四02));
list.add(new User(5, 王五, 我是王五01));
list.add(new User(6, 王五, 我是王五02));2. List转Map(无序-默认)
List转Map有两种Map格式分别是 MapString, User和 MapString, List下面我将分别展示
2.1 List转MapString, User
转换成MapString, User我们需要使用到Collectors.toMap方法
//通过名字进行分组如果名字重复的话只取第一个List转MapString, User
MapString, User map01 list.stream().collect(Collectors.toMap(User::getName, Function.identity(), (u1, u2) - u1));
System.out.println(map01);执行结果
{
李四User(id3, name李四, note我是李四01),
张三User(id1, name张三, note我是张三01),
王五User(id5, name王五, note我是王五01)
}2.2 List转MapString, ListUser
转换成MapString, ListUser我们需要使用到Collectors.groupingBy方法
//通过名字进行分组如果名字重复的话就分组成ListList转MapString, ListUser
MapString, ListUser map02 list.stream().collect(Collectors.groupingBy(User::getName));
System.out.println(map02);执行结果
{
李四[User(id3, name李四, note我是李四01), User(id4, name李四, note我是李四02)],
张三[User(id1, name张三, note我是张三01), User(id2, name张三, note我是张三02)],
王五[User(id5, name王五, note我是王五01), User(id6, name王五, note我是王五02)]
}我们可以看到map中打印出来的执行结果并没有按照List中添加的顺序打印的
3. List转LinkedHashMap(有序)
List转LinkedHashMap也有两种Map格式分别是 LinkedHashMapString, User和 LinkedHashMapString, List下面我将分别展示
3.1 List转LinkedHashMapString, User
转换成LinkedHashMapString, User我们需要使用到Collectors.toMap方法
//通过名字进行分组如果名字重复的话只取第一个List转LinkedHashMapString, User
MapString, User map03 list.stream().collect(Collectors.toMap(User::getName, Function.identity(), (u1, u2) - u1, LinkedHashMap::new));
System.out.println(map03);执行结果
{
张三User(id1, name张三, note我是张三01),
李四User(id3, name李四, note我是李四01),
王五User(id5, name王五, note我是王五01)
}3.2 List转LinkedHashMapString, ListUser
转换成LinkedHashMapString, ListUser我们需要使用到Collectors.groupingBy方法
//通过名字进行分组如果名字重复的话就分组成ListList转LinkedHashMapString, ListUser
MapString, ListUser map04 list.stream().collect(Collectors.groupingBy(User::getName, LinkedHashMap::new, Collectors.toList()));
System.out.println(map04);执行结果
{
张三[User(id1, name张三, note我是张三01), User(id2, name张三, note我是张三02)],
李四[User(id3, name李四, note我是李四01), User(id4, name李四, note我是李四02)],
王五[User(id5, name王五, note我是王五01), User(id6, name王五, note我是王五02)]
}我们可以看到map中打印出来的执行结果是按照List中添加的顺序打印的
4. 总结
在List转Map的过程中 如果我们对Map中的顺序没要求我们可以通过stream流将List转换为默认的HMap即可 如果我们对Map中的顺序有要求我们可以通过stream流将List转换为LinkedHashMap才行
5. 全部代码
public static void test1() {ListUser list new ArrayList();list.add(new User(1, 张三, 我是张三01));list.add(new User(2, 张三, 我是张三02));list.add(new User(3, 李四, 我是李四01));list.add(new User(4, 李四, 我是李四02));list.add(new User(5, 王五, 我是王五01));list.add(new User(6, 王五, 我是王五02));//通过名字进行分组如果名字重复的话只取第一个List转MapString, UserMapString, User map01 list.stream().collect(Collectors.toMap(User::getName, Function.identity(), (u1, u2) - u1));System.out.println(map01);//通过名字进行分组如果名字重复的话就分组成ListList转MapString, ListUserMapString, ListUser map02 list.stream().collect(Collectors.groupingBy(User::getName));System.out.println(map02);//通过名字进行分组如果名字重复的话只取第一个List转LinkedHashMapString, UserMapString, User map03 list.stream().collect(Collectors.toMap(User::getName, Function.identity(), (u1, u2) - u1, LinkedHashMap::new));System.out.println(map03);//通过名字进行分组如果名字重复的话就分组成ListList转LinkedHashMapString, ListUserMapString, ListUser map04 list.stream().collect(Collectors.groupingBy(User::getName, LinkedHashMap::new, Collectors.toList()));System.out.println(map04);
}public class User {private Integer id;private String name;private String note;
}