list转map

方法一:

List  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 maps = Maps.uniqueIndex(userList, new Function() {
            @Override
            public Long apply(User user) {
                return user.getId();
            }
   });

方法二:

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

你可能感兴趣的:(list转map)