java将List集合对象中的两个属性转化为Map

    public void listToMap() {
        List userList = new ArrayList<>();
        User user1 = new User();
        user1.setId("1");
        user1.setAccount("zhangsan");
        userList.add(user1);
        User user2 = new User();
        user2.setId("2");
        user2.setAccount("lisi");
        userList.add(user2);
        User user3 = new User();
        user3.setId("3");
        user3.setAccount("wangwu");
        userList.add(user3);
        Map userMap = userList.stream().collect(Collectors.toMap(User::getId, User::getAccount));
        System.out.println(userMap);//{1=zhangsan, 2=lisi, 3=wangwu}
    }

你可能感兴趣的:(java,list,开发语言)