• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

stream流 list转map

开发技术 开发技术 3小时前 2次浏览

 

stream流 list转map

 

 

 

package com.mayikt.stream;

import com.mayikt.entity.UserEntity;

import java.util.ArrayList;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName Test02
 * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
 * @Version V1.0
 **/
public class Test02 {
    public static void main(String[] args) {
        ArrayList<UserEntity> userEntities = new ArrayList<>();
        userEntities.add(new UserEntity("mayikt", 20));
        userEntities.add(new UserEntity("meite", 28));
        userEntities.add(new UserEntity("zhangsan", 35));

        // list集合只有元素值 key list转换成map集合的情况下 指定key--user对象 username属性  value user对象
        /**
         * map<String(userName),UserEntity>
         */
        Stream<UserEntity> stream = userEntities.stream();
        /**
         * new Function<UserEntity(list集合中的类型), String(key map)>
         */
        Map<String, UserEntity> collect = stream.collect(Collectors.toMap(userEntity -> userEntity.getUserName(), userEntity -> userEntity));
        collect.forEach((BiConsumer) (o, o2) -> System.out.println(o + "," + o2));
    }
}

 


程序员灯塔
转载请注明原文链接:stream流 list转map
喜欢 (0)