2016-05-16 2 views
1

У меня есть список, который я хочу преобразовать в список.convert list on map

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.

Спасибо за помощь заранее!

+1

Попробуйте писать код, а не дает нам подсказки –

+0

см [http://stackoverflow.com/questions/31693781/convert-string-array-to-map-using-java-8-лямбда-выражения] при использовании 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]