list转map

2018-11-15  本文已影响0人  酷酷的美猴王

方法一:

List<User>  userList = new ArrayList<>();
       User user = new User();
        user.setId(1L);
        user.setUsercode("123");
        user.setName("zhang");
        userList.add(user);

        User user1 = new User();
        user1.setId(2L);
        user1.setUsercode("456");
        user1.setName("li");
        userList.add(user1);

        User user2 = new User();
        user2.setId(5L);
        user2.setUsercode("789");
        user2.setName("li");
        userList.add(user2); 
Map<Long, User> maps = Maps.uniqueIndex(userList, new Function<User, Long>() {
            @Override
            public Long apply(User user) {
                return user.getId();
            }
   });

方法二:

List<ChoosePersonDto> choosees = new ArrayList<>();
Map<String, ChoosePersonDto> map =choosees.stream().
                collect(Collectors.toMap(ChoosePersonDto::getRole, choosePersonDto -> choosePersonDto));
上一篇下一篇

猜你喜欢

热点阅读