I have a list of Students that i get it from the database. I want, to structure that list, according to the age of the students, and, at the end, to have a list like this:
[
30 => [
"id1" => "Name1",
"id2" => "Name2",
],
31 => [
"id5" => "Name3",
"id6" => "Name4",
]
]
in my code, i recieve the list of students like this:
List<ApplicantModel> applicants = applicantDao.getApplicantsByApplicationStatus(applicantTypeId);
and i want here the collection to be created inside "for":
for(int i=0;i<=applicants.size()-1;i++){
//code here to create the collection with the above structure
}
or, if you have other better suggestion? Thanks!
You could use TreeMap for that (according the order by age and id): It could look like this: (I don't know the exact names of methods to get age and id - so you may need to adjust the getAge and getId method calls):
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;
}
}
}
This code will print:
{30={id1=name1, id2=name2}, 31={id5=name3, id6=name4}}
You can also create the structure within a map and lists:
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);
}
A result similar to the one Krzysztof Cichocki is getting can also be obtained with streams (Java 8 or later):
Map<Integer, Map<String, String>> byAge = studentList.stream()
.collect(Collectors.groupingBy(ApplicantModel::getAge,
Collectors.toMap(ApplicantModel::getId, ApplicantModel::getName)));
This produces (in a not very reader-friendly format, I admit):
{30={id2=name2, id1=name1}, 31={id6=name4, id5=name3}}
If you specifically want TreeMap
s, use the three-argument groupingBy()
and/or the four-argument 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)));
Now the elements come out sorted from toString()
:
{30={id1=name1, id2=name2}, 31={id5=name3, id6=name4}}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.