java - 迭代 ArrayList 并添加到具有特定格式的另一个 arrayList
问题描述
添加到 ArrayList 的有效方法
我有一个arrayList,如下所示:
Name id zone cases
Test 12 Above 20
Test1 13 In 30
Test1 13 Above 12
Test1 13 Below 10
Test1 14 Above 2
Test1 14 In 8
Test1 14 Below 6
我必须迭代这个 arrayList 并创建这种类型的结果列表
Name id zone cases
Test 12 Above 20
Test 12 In 0
Test 12 Below 0
Total 20
Test1 13 Above 30
Test1 13 In 12
Test1 13 Below 10
Total 52
Test1 14 Above 2
Test1 14 In 8
Test1 14 Below 6
Total 16
虽然名称 'Test' 没有 'In' 和 'Below' 我必须将这些值添加到 0 个案例的列表中。它应该是动态迭代,但 'Above'、'In' 和 'Below' 是静态的。name 也会有多个 id,比如 test1 有 id 13 和 14。每个部分都应该有一个名为“Total”的部分添加到列表中。请问有什么建议吗?
解决方案
你的课程应该是这样的。这就是你正在寻找的。如果您愿意,只需放置一些独特的 id 逻辑即可。如果 Level 刚好在上方/下方/下方,请执行更多逻辑处理。在开始时初始化它们并更新这些数据
公共枚举级别{
Above("Above"),
In("In"),
Below("Below");
private String level;
Level(String level) {
this.level =level;
}
public String getLevel() {
return level;
}
}
公共类测试{
private String name;
private Integer id;
private Level level;
private Integer count;
public Test(String name, Integer id, Level level, Integer count) {
this.name = name;
this.id = id;
this.level = level;
this.count = count;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Level getLevel() {
return level;
}
public void setLevel(Level level) {
this.level = level;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
@Override
public String toString() {
return "Test [name=" + name + ", id=" + id + ", level=" + level
+ ", count=" + count + "]";
}
}
导入 java.util.HashSet;导入 java.util.Set;
公共类聚合器{
private Set<Test> testList;
private int totalCount;
public Aggregator() {
if(testList==null)
testList = new HashSet<Test>();
totalCount=0;
}
public void addTest(Test test) {
testList.add(test);
totalCount = totalCount + test.getCount();
}
public Set<Test> getTestList() {
return testList;
}
@Override
public String toString() {
return "Aggregator [testList=" + testList + ", totalCount="
+ totalCount + "]";
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
}
导入 java.util.ArrayList;导入 java.util.List;
公共类结构{
private List<Aggregator> data;
public Structure() {
if(data==null) {
data = new ArrayList<Aggregator>();
}
}
public List<Aggregator> getData() {
return data;
}
}
导入 java.util.List;
公共类 AggregatorTest {
public static void main(String[] args) {
Structure structure = new Structure();
List<Aggregator> finalData = structure.getData();
Test test = new Test("Test", 1, Level.Above, 10);
Test test1 = new Test("Test", 1, Level.In, 10);
Test test2 = new Test("Test1", 2, Level.Above, 10);
Test test3 = new Test("Test1", 2, Level.In, 10);
Aggregator aggregator = new Aggregator();
aggregator.addTest(test);
aggregator.addTest(test1);
Aggregator aggregator2 = new Aggregator();
aggregator2.addTest(test2);
aggregator2.addTest(test3);
finalData.add(aggregator);
finalData.add(aggregator2);
for (Aggregator entry : finalData) {
System.out.println(entry);
}
}
}
输出:
聚合器 [testList=[Test [name=Test, id=1, level=Above, count=10], Test [name=Test, id=1, level=In, count=10]], totalCount=20] 聚合器 [ testList=[Test [name=Test1, id=2, level=Above, count=10], Test [name=Test1, id=2, level=In, count=10]], totalCount=20]
推荐阅读
- sql - 如何从 BigQuery 中的另一个表中找到其中一个值的百分比?
- android - Android 受信任的 Web 应用程序:在输入类型 = 文件中不过滤,而在 Web 应用程序中过滤
- javascript - 我怎样才能得到一个 div 及其所有子级及其各自的计算样式?
- javascript - jquery .animate 和 .effect 函数在 Angular 8 组件中不起作用
- typescript - 来自对象数组的打字稿模板文字类型
- azure - 如何检查 Powershell cmdlet Get-AzKeyVaultSecret 是否支持 -AsPlainText 参数?
- dataframe - 如何使用来自另一个数据帧的随机值更新 PySpark 中的数据帧?
- python - 在第二帧中添加后退按钮(Tkinter)
- elasticsearch - 弹性搜索加入
- webpack - React + Material UI + Storybook + Yarn Monorepo - 主题恢复默认