0

I am attempting to generate an ArrayList by merging two other lists. I am allowed duplicate objects, however my resulting ArrayList must contain the difference between both initial lists. I realize this may sound convoluted, so here is an example:

ArrayList 1: [obj1, obj1, obj1, obj2, obj4, obj4]
ArrayList 2: [obj1, obj2, obj2, obj3]

Resulting ArrayList: [obj1, obj1, obj2, obj3, obj4, obj4]

I feel like this should be simple but I cannot seem to figure it out. I would use ArrayList1.removeAll(ArrayList2), however each object has its' own individual ID so I don't think I would detect that they are the same object.

EDIT: fixed an error in my resulting ArrayList
Thanks!

17
  • 3
    This sounds a lot like a homework question. If you are having trouble with your homework you should ask you teacher/professor/student aid/etc. That is what they are paid for. Commented Mar 29, 2016 at 13:05
  • 1
    Have you tried anything yet? Commented Mar 29, 2016 at 13:06
  • 1
    HInt : This assignment is to test you loops and counter skills :) Commented Mar 29, 2016 at 13:07
  • 1
    yeah you need to at least have a go. Post some code and maybe someone will give you a hand. Commented Mar 29, 2016 at 13:10
  • Not an assignment. I simplified a problem I am having while working on part of a scheduler. The objects are appointments. Commented Mar 29, 2016 at 13:11

1 Answer 1

1

Simply use a hashmap, mapping an element to the number of times it occurred in list1, and another hashmap for list2, then make a new arraylist and add objx n times, where n = abs(hashmap1.get(objx) - hashmap2.get(objx)).

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        List<Integer> list1 = Arrays.asList(new Integer[] { 1, 1, 1, 2, 4, 4 });
        List<Integer> list2 = Arrays.asList(new Integer[] { 1, 2, 2, 3 });
        HashMap<Integer, Integer> hashMap1 = new HashMap<>();
        HashMap<Integer, Integer> hashMap2 = new HashMap<>();
        for (Integer i : list1) {
            if (hashMap1.containsKey(i)) {
                hashMap1.put(i, hashMap1.get(i) + 1);
            } else {
                hashMap1.put(i, 1);
            }
        }
        for (Integer i : list2) {
            if (hashMap2.containsKey(i)) {
                hashMap2.put(i, hashMap2.get(i) + 1);
            } else {
                hashMap2.put(i, 1);
            }
        }
        HashSet<Integer> dedup = new HashSet<>();
        for (Integer i : list1) {
            dedup.add(i);
        }
        for (Integer i : list2) {
            dedup.add(i);
        }
        ArrayList<Integer> result = new ArrayList<>();
        for (Integer i : dedup) {
            Integer n1 = hashMap1.get(i);
            Integer n2 = hashMap2.get(i);
            int n = Math.abs((n1 == null ? 0 : n1) - (n2 == null ? 0 : n2));
            for (int j = 0; j < n; ++j) {
                result.add(i);
            }
        }
        for (Integer i : result) {
            System.out.println(i);
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

OK, this seems to be what the asker was looking for... Well, two minor remarks: The contents of the first for loops may be written more concisely as hashMap.merge(i, 1, Integer::sum);, and the second ones may be replaced with calls to dedup.addAll(list);.
@Marco13 thanks. You can edit if you want, I'm not familiar with java8 yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.