[英]Collections - add elements to a collection
我有一个从数据库中获取的学生列表。 我想要根据学生的年龄来构造该列表,最后要有一个像这样的列表:
[
30 => [
"id1" => "Name1",
"id2" => "Name2",
],
31 => [
"id5" => "Name3",
"id6" => "Name4",
]
]
在我的代码中,我收到这样的学生列表:
List<ApplicantModel> applicants = applicantDao.getApplicantsByApplicationStatus(applicantTypeId);
我想在这里在“ for”内创建集合:
for(int i=0;i<=applicants.size()-1;i++){
//code here to create the collection with the above structure
}
或者,如果您还有其他更好的建议? 谢谢!
您可以为此使用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}}
您还可以在地图和列表中创建结构:
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);
}
也可以通过流(Java 8或更高版本)获得与Krzysztof Cichocki相似的结果:
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
,请使用三参数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}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.