将列表转换为地图

时间:2016-05-16 11:18:07

标签: java dictionary arraylist

我有以下列表,我想将列表转换为地图。

SurveyAllocationUsers user1 = new SurveyAllocationUsers();
    user1.setSurveyorId("1");
    user1.setSurveyorTypeCode("LSR");

    SurveyAllocationUsers user2 = new SurveyAllocationUsers();
    user2.setSurveyorId("1");
    user2.setSurveyorTypeCode("SR");

    SurveyAllocationUsers user3 = new SurveyAllocationUsers();
    user3.setSurveyorId("2");
    user3.setSurveyorTypeCode("LSR");

    SurveyAllocationUsers user4 = new SurveyAllocationUsers();
    user4.setSurveyorId("2");
    user4.setSurveyorTypeCode("SR");

    SurveyAllocationUsers user5 = new SurveyAllocationUsers();
    user5.setSurveyorId("2");
    user5.setSurveyorTypeCode("BG");

    List<SurveyAllocationUsers> list = new ArrayList<SurveyAllocationUsers>();

    list.add(user1);list.add(user2);list.add(user3);list.add(user4);list.add(user5);

想要将列表转换为如下所示的地图。

Map<String,List<String>> usersMap = new  HashMap<String, List<String>>();

地图键将是SurveyorId,值将与SurveyorTypeCode列表相对应。

提前感谢您的帮助!!!

6 个答案:

答案 0 :(得分:0)

应该是下面的内容

userMap.put(user1.getSurveyorId(), new ArrayList<>().add(user1.getSurveyorTypeCode));

userMap.put(user2.getSurveyorId(), new ArrayList<>().add(user2.getSurveyorTypeCode));

答案 1 :(得分:0)

它应该是这样的:

List<SurveyAllocationUsers> list = new ArrayList<SurveyAllocationUsers>();

list.add(user1);list.add(user2);list.add(user3);list.add(user4);list.add(user5);
Map<String,List<String>> usersMap = new  HashMap<String, List<String>>();

for(SurveyAllocationUsers sau: list){
   if(!userMap.contains(sau.getSurveyorId())){
    userMap.put(sau.getSurveyorId(), new ArrayList<>().add(sau.getSurveyorTypeCode));
   }else{
    userMap.get(sau.getSurveyorId()).add(sau.getSurveyorTypeCode);
   }
}

注意:代码未合规。

答案 2 :(得分:0)

有点难看,但解决了你的问题:

Map<String, List<String>> map = list.stream().collect(toMap(SurveyAllocationUsers::getSurveyorId, p -> new ArrayList(singletonList(p.getSurveyorTypeCode())), (s, a) -> {               
            s.addAll(a);
            return s;
        })); 

答案 3 :(得分:0)

你可以试试这个:

Map<String, List<String>> usersMap = new HashMap<String, List<String>>();
for (SurveyAllocationUsers user : list) {
    List<String> typeCodes = new ArrayList<>();
    typeCodes.add(user.getSurveyorTypeCode());
    usersMap.put(user.getSurveyorId(), typeCodes);
}

答案 4 :(得分:0)

感谢大家的回复,我得到了如下解决方案。

List <String>newUserList = null;
    Map<String,List<String>> usersMap = new  HashMap<String, List<String>>();       
    for(SurveyAllocationUsers sau: list){
        if(usersMap.containsKey(sau.getSurveyorId())){

            List <String>myList = usersMap.get(sau.getSurveyorId());
            myList.add(sau.getSurveyorTypeCode());
            usersMap.put(sau.getSurveyorId(), myList);

        }else{
            if(sau.getSurveyorId() != null){
                newUserList = new ArrayList<String>();
                newUserList.add(sau.getSurveyorTypeCode());
                usersMap.put(sau.getSurveyorId(), newUserList);
            }

        }
    }

按预期回答 1 [LSR,SR] 2 [LSR,SR,BG]

答案 5 :(得分:0)

使用Java8应该是这样的

resMap = list.stream().collect(Collectors.groupingBy(SurveyAllocationUsers::getSurveyorId,
                Collectors.mapping(SurveyAllocationUsers::getSurveyorTypeCode,Collectors.toList())));
  

输出:{1 = [LSR,SR],2 = [LSR,SR,BG]}