iterating an array list and adding a specific format to another array list

Your Classes should be like this. This is what you are looking. Just put some unique id logic if you want like that. DO some more logic process if Level are just Above/In/Below. Initilaize them on beginning and update data on those

public enum Level {

Above("Above"),
In("In"),
Below("Below");

private String level;

Level(String level) {
    this.level =level;
}

public String getLevel() {
    return level;
}

}

public class Test {

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 + "]";
}

}

import java.util.HashSet; import java.util.Set;

public class Aggregator {

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;
}

}

import java.util.ArrayList; import java.util.List;

public class Structure {

private List<Aggregator> data;

public Structure() {
    if(data==null) {
        data = new ArrayList<Aggregator>();
    }
}

public List<Aggregator> getData() {
    return data;
}

}

import java.util.List;

public class 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);
    }

}

}

Output:

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]

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top