0

I have a an Arraylist of Arraylists. First I am trying to compare all the arraylists and if the first elements are equal, merge all duplicate arraylists into one and sum the last elements of each. Each arraylist has a fixed size of 3 elements.

For example :

ArrayList<String> archiveList = new ArrayList<String>();
        archiveList.add("2605");
        archiveList.add("SD");
        archiveList.add("25");
ArrayList<String> archiveList2 = new ArrayList<String>();
        archiveList2.add("3470");
        archiveList2.add("SD2");
        archiveList2.add("25");   
ArrayList<String> archiveList3 = new ArrayList<String>();
        archiveList3.add("2605");
        archiveList3.add("SD2");
        archiveList3.add("20");
ArrayList<String> archiveList4 = new ArrayList<String>();
        archiveList4.add("2605");
        archiveList4.add("SD2");
        archiveList4.add("20");

//code to consolidate arraylists

ArrayList<ArrayList<String>> dataTable = new ArrayList<ArrayList<String>>();
           dataTable.addRow(archiveList);

I would like the end result to become archiveList = ["2605","SD","65"]; // last element is the sum from prev arrays(25+25+20) archiveList = ["3470","SD2","20"]

Thank you in advance and a Happy New Year to everybody !

5
  • Your specification is too vague. When you say "some are equal" it appears it has to be the first one, not any one or any two. Why do you some the last value as a String but nother other values or other elements? Commented Jan 1, 2015 at 22:24
  • I think your best bet is to create a POJO because it seems like there is a relationship between the values you store in each ArrayList. Then just merge the one that are equals (you'll need to override it) and create a new object which has the sum of this "third" parameter. Commented Jan 1, 2015 at 22:26
  • 1
    dataTable.addAll(archiveList,archiveList2,archive3,archiveList4) is not even valid code. There is no varargs version of addAll. Commented Jan 1, 2015 at 22:29
  • Sorry for misleading code, I re-edited it. It's just a regular addRow method. Commented Jan 1, 2015 at 22:39
  • @Marquis - did you find a suitable solution? Commented Jan 8, 2015 at 14:10

2 Answers 2

1

The following Java 8 solution may solve your problem:

// First, group the lists by the "id" i.e. the first entry in each list
final Map<String, List<List<String>>> groupedLists =
        Stream.of(archiveList1, archiveList2, archiveList3, archiveList4)
                .collect(Collectors.groupingBy(l -> l.get(0)));

// Now, setup the result
final List<List<String>> result = groupedLists.values()
        .stream()
        .map(ll -> {
            String first = ll.get(0).get(0);        // The key e.g. "2605"
            String second = ll.get(0).get(1);       // The second element e.g. "SD"

            // Summarize the lists with the same "id"
            int sum = ll.stream()
                    .map(archive -> archive.get(2)) // This is the "number"
                    .mapToInt(Integer::parseInt)    // convert it to an int
                    .sum();                         // And summarize

            // This list holds the aggregated result
            List<String> aggregate = new ArrayList<>();
            aggregate.add(first);
            aggregate.add(second);
            aggregate.add(Integer.valueOf(sum).toString());
            return aggregate;
        })
        .collect(Collectors.toList());              // Finally, collect the whole thing to a list of lists
Sign up to request clarification or add additional context in comments.

1 Comment

I really like your idea wassgreen. Unfortunately I can't implement it in production since we don't use Java 8 yet. Will try it out on my own though. Thank you.
0

You can use HashSet,in which all ArrayList element can be added into this HashSet. At the end you can see that same elements will be merged .in other words there is only one element exist in whilo set.

1 Comment

A HashSet merges something? Can you provide an example?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.