2

so I have 2 large arraylists of objects of A 500k and B 900k in size and I want to create a 3rd list (c) of all the dups of A in B.

So I've been doing

    for(obj as:B)
    {

        if(!A.contains(as.getA()))
        {
            C.add(as);
        }

    }

But this takes quite a bit of time to process. Would maps or sets be quicker or any other suggestions? Thanks.

2
  • 1
    Check this out stackoverflow.com/questions/8379510/… Commented Mar 20, 2017 at 17:07
  • If they are sortable, sort them first then use a binary search (see Collections class). If you can deal with the overhead of putting them in a Set then see the answer by @Kushan. Commented Mar 20, 2017 at 17:11

1 Answer 1

3

Use HashSet collection

List<MyClass> myList = new ArrayList<MyClass>();
myList.addAll(listA);
myList.addAll(listB);

Set<MyClass> mySet = new HashSet<MyClass>(myList);
Sign up to request clarification or add additional context in comments.

2 Comments

Since duplicates are desired should use myList.retainAll(listB) ?
retainAll means removes from the list all of its elements that are not contained in the specified collection

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.