我有以下列表,我想將列表轉換爲地圖。將列表轉換成地圖
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列表。
感謝您的幫助!
試着寫代碼,而不是給我們一些提示 –
參見[http://stackoverflow.com/questions/31693781/convert-string-array-to-map-using-java-8-lambda-expressions]如果使用java 8。 – k1133