2016-05-16 63 views
1

我有以下列表,我想將列表轉換爲地圖。將列表轉換成地圖

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

map key將SurveyorId和values對應的SurveyorTypeCode列表。

感謝您的幫助!

+1

試着寫代碼,而不是給我們一些提示 –

+0

參見[http://stackoverflow.com/questions/31693781/convert-string-array-to-map-using-java-8-lambda-expressions]如果使用java 8。 – k1133

回答

0

它應該像下面

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

userMap.put(user2.getSurveyorId(), new ArrayList<>().add(user2.getSurveyorTypeCode)); 
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); 
    } 
} 

注:代碼中不遵守。

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