List 集合通过创建stream 流的方式转成map集合

简介: List 集合通过创建stream 流的方式转成map集合

public class test7 {
    public static void main(String[] args) {
        ArrayList<UserEntity> list = new ArrayList<>();
        list.add(new UserEntity("xialijun",12));
        list.add(new UserEntity("xia",24));
        list.add(new UserEntity("qwer",1234));
        list.add(new UserEntity("qwet",1223));
        list.add(new UserEntity("qwey",1235));
        list.add(new UserEntity("qweye",1235));
        //流: stream  串行流  单线程
        //    parallelStream()  并行流  多线程  效率高
        //
        Set<UserEntity> collect = list.stream().collect(Collectors.toSet());
        collect.forEach(x->{
            System.out.println(x.toString());
        });
        System.out.println("========================================");
        collect.forEach(System.out::println);
        Set<UserEntity> collect1 = list.parallelStream().collect(Collectors.toSet());
        //key     
        //UserEntity   list  集合中的数据
      第一种写法 :
//        Map<String, UserEntity> map = list.stream().collect(Collectors.toMap(new Function<UserEntity, String>() {
//            @Override
//            public String apply(UserEntity userEntity) {
//                return userEntity.getName();
//            }
//            //value
//        }, new Function<UserEntity, UserEntity>() {
//            @Override
//            public UserEntity apply(UserEntity userEntity) {
//                return userEntity;
//            }
//        }));
//        map.forEach(new BiConsumer<String, UserEntity>() {
//            @Override
//            public void accept(String s, UserEntity userEntity) {
//                System.out.println(s + "====" + userEntity);
//            }
//        });
       第二种写法
        Map<String, UserEntity> map = list.stream()
                //key----->UserEntity::getName,
                //value----->    userEntity -> userEntity
                .collect(Collectors.toMap(UserEntity::getName,
                        userEntity -> userEntity));
        map.forEach((s, userEntity) -> System.out.println(s + "====" + userEntity));
    }
}
目录
相关文章
|
2月前
|
存储 安全 Java
java集合框架及其特点(List、Set、Queue、Map)
java集合框架及其特点(List、Set、Queue、Map)
|
1天前
|
存储 安全 算法
Java一分钟之-Java集合框架入门:List接口与ArrayList
【5月更文挑战第10天】本文介绍了Java集合框架中的`List`接口和`ArrayList`实现类。`List`是有序集合,支持元素重复并能按索引访问。核心方法包括添加、删除、获取和设置元素。`ArrayList`基于动态数组,提供高效随机访问和自动扩容,但非线程安全。文章讨论了三个常见问题:索引越界、遍历时修改集合和并发修改,并给出避免策略。通过示例代码展示了基本操作和安全遍历删除。理解并正确使用`List`和`ArrayList`能提升程序效率和稳定性。
7 0
|
2天前
|
存储 安全 Java
Java容器类List、ArrayList、Vector及map、HashTable、HashMap
Java容器类List、ArrayList、Vector及map、HashTable、HashMap
|
5天前
|
JSON 数据格式
使用 Gson 将 Map、List等转换为json string
使用 Gson 将 Map、List等转换为json string
12 0
|
16天前
|
Java API
List转Map(id为key,list为value)
List转Map(id为key,list为value)
9 0
|
1月前
|
存储 Java API
java集合Collection(List)和泛型
java集合Collection(List)和泛型
|
1月前
|
算法
递归淘汰List集合头部数据,获取最终集合的起始坐标
递归淘汰List集合头部数据,获取最终集合的起始坐标
|
1月前
|
Java
list集合 使用java8同一列表获取前一条的数据放到当前对象中
list集合 使用java8同一列表获取前一条的数据放到当前对象中
|
1月前
|
存储 安全 Java
Java集合详解(List、Map、Set)
Java集合详解(List、Map、Set)
34 4
|
1月前
|
存储 C语言 C++
C++中STL常用容器(vector、deque、list、map、set)一文带你了解
C++中STL常用容器(vector、deque、list、map、set)一文带你了解