2017-04-10 128 views
0

我有一個我從數據庫中獲得的學生列表。我想,以結構清單,根據學生的年齡,並在年底,能有這樣的名單:收藏夾 - 添加元素到集合

[ 
    30 => [ 
    "id1" => "Name1", 
    "id2" => "Name2", 
    ], 
    31 => [ 
    "id5" => "Name3", 
    "id6" => "Name4", 
    ] 
] 
在我的代碼

,我收到的學生這樣的名單:

List<ApplicantModel> applicants = applicantDao.getApplicantsByApplicationStatus(applicantTypeId); 

,我想在這裏待內部創建的集合「爲」:

for(int i=0;i<=applicants.size()-1;i++){ 
    //code here to create the collection with the above structure 
} 

,或者如果你有其他更好的建議? 謝謝!

+0

你的問題是不明確的,你要根據學生的年齡排序您ApplicantModel'的'名單?請提供'ApplicantModel'對象的內容 – VPK

+0

'=>'表示您想要的結果是什麼? –

回答

1

你可以使用TreeMap(根據年齡和id的順序): 它可能看起來像這樣:(我不知道獲取年齡和id的方法的確切名稱 - 所以你可能需要調整的getAge和的getId方法調用):

import java.util.LinkedList; 
import java.util.List; 
import java.util.TreeMap; 

public class ApplicantStructureTest { 

    public static void main(String[] args) { 
     List<ApplicantModel> applicants = new LinkedList<>(); 
     applicants.add(new ApplicantModel("id1", 30, "name1")); 
     applicants.add(new ApplicantModel("id2", 30, "name2")); 
     applicants.add(new ApplicantModel("id5", 31, "name3")); 
     applicants.add(new ApplicantModel("id6", 31, "name4")); 

     // here is your for loop 
     TreeMap<Integer, TreeMap<String, String>> structured = new TreeMap<Integer, TreeMap<String, String>>(); 
     for (int i = 0; i <= applicants.size() - 1; i++) { 
      ApplicantModel applicant = applicants.get(i); 
      Integer age = applicant.getAge(); 
      TreeMap<String, String> ageMap = structured.get(age); 
      if (ageMap == null) { 
       ageMap = new TreeMap<String, String>(); 
       structured.put(age, ageMap); 
      } 
      ageMap.put(applicant.getId(), applicant.getName()); 
     } 


     System.out.println(structured); 
    } 

    public static class ApplicantModel { 
     private Integer age; 
     private String id; 
     private String name; 

     public ApplicantModel(String id, Integer age, String name) { 
      this.id = id; 
      this.age = age; 
      this.name = name; 
     } 

     public String getName() { 
      return name; 
     } 

     public Integer getAge() { 
      return age; 
     } 

     public String getId() { 
      return id; 
     } 

    } 

} 

此代碼將打印:

{30 = {ID1 = NAME1,ID2 = NAME2},31 = {ID5 = NAME3,ID6 = NAME4}}

1

您還可以在地圖和列表中創建結構:

Map<Integer,List<ApplicantModel>> structured = new HashMap<Integer, List<ApplicantModel>>(); 
for(int i = 0; i <= applicants.size() - 1; i++){ 
    ApplicantModel applicant = applicants.get(i); 

    Integer age = applicant.getAge(); 
    List<ApplicantModel> list = structured.get(age); 
    if (list == null) { 
    list = new List<ApplicantModel>(); 
    structured.put(age, list); 
    } 
    list.add(applicant); 
} 
0

的結果類似於一個的Krzysztof Cichocki越來越也可以與流(爪哇8或更高版本)中得到:

Map<Integer, Map<String, String>> byAge = studentList.stream() 
      .collect(Collectors.groupingBy(ApplicantModel::getAge, 
              Collectors.toMap(ApplicantModel::getId, ApplicantModel::getName))); 

這產生(在一個不是很方便讀者的格式,我承認):

{30={id2=name2, id1=name1}, 31={id6=name4, id5=name3}} 

如果你特別希望TreeMap S,使用三個參數groupingBy()和/或四參數toMap()

Map<Integer, Map<String, String>> byAge = studentList.stream() 
      .collect(Collectors.groupingBy(ApplicantModel::getAge, 
              TreeMap::new, 
              Collectors.toMap(ApplicantModel::getId, 
                  ApplicantModel::getName, 
                  (u, v) -> { throw new IllegalArgumentException(); }, 
                  TreeMap::new))); 

現在的元素出來排序從toString()

{30={id1=name1, id2=name2}, 31={id5=name3, id6=name4}}