繁体   English   中英

迭代 ArrayList 并添加到具有特定格式的另一个 arrayList

[英]Iterating ArrayList and adding to another arrayList with specific format

添加到 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' 是静态的。 此外,名称将有多个 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);
    }

}

}

输出:

Aggregator [testList=[Test [name=Test, id=1, level=Above, count=10], Test [name=Test, id=1, level=In, count=10]], totalCount=20] Aggregator [ testList=[Test [name=Test1, id=2, level=Above, count=10], Test [name=Test1, id=2, level=In, count=10]], totalCount=20]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM