【问题标题】:convert list into map将列表转换为地图
【发布时间】:2016-05-16 11:18:06
【问题描述】:

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

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列表。

提前感谢您的帮助!!!

【问题讨论】:

标签: java dictionary arraylist


【解决方案1】:

应该像下面这样

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

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

【讨论】:

    【解决方案2】:

    应该是这样的:

    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);
       }
    }
    

    注意:未遵守代码。

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        你可以试试这个:

        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);
        }
        

        【讨论】:

          【解决方案5】:

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

          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]

          【讨论】:

            【解决方案6】:

            使用Java8应该是这样的

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

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-11-01
              • 2020-07-06
              • 1970-01-01
              • 2015-07-10
              • 1970-01-01
              • 2022-06-10
              • 2022-01-01
              相关资源
              最近更新 更多