Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Duplicates from ArrayList of ArrayLists [duplicate]

I have a problem that I am sure the solution is so easy but I am not able to find it. I have an ArrayList of smaller ArrayLists. Those lists contain elements of type String. I want to merge the smaller lists to one and then remove the duplicates. Let me be clear.

I have this:

[[USA, Maine], [USA, Maine, Kennebunk], [USA, Maine, North Berwick], 
[USA, New Hampshire], [USA, Keene, New Hampshire], [USA, Keene, New 
Hampshire, Main Street], [USA, New Hampshire, Swanzey]].

This is my main list that has smaller lists inside. I want to have a final ArrayList which is the merge of the smaller ones and get the duplicates deleted.

What I want is:

[USA, Maine, Kennebunk, North Berwick, New Hampshire , Keene, Main Street, Swanzey]

Any help is appreciated. Thank you

like image 233
hristoforidisc Avatar asked Dec 15 '17 17:12

hristoforidisc


People also ask

Can Arraylists have duplicates?

Duplicates : ArrayList allows duplicate values while HashSet doesn't allow duplicates values. Ordering : ArrayList maintains the order of the object in which they are inserted while HashSet is an unordered collection and doesn't maintain any order.


2 Answers

This is a concise solution using the Stream class:

listOfLists.stream().flatMap(List::stream).collect(Collectors.toSet())

Note that the result is of type Set. This takes care of removing the duplicates.

If you need a List, you can use this:

listOfLists.stream()
           .flatMap(List::stream)
           .distinct()
           .collect(Collectors.toList())

Note that this even guarantees the order of elements to be stable, i.e. [["foo","bar"],["bar","abc","foo"]] will always result in ["foo","bar","abc"] in this order. Most solutions using Set do not guarantee this because most of them are not sorted.

like image 111
siegi Avatar answered Oct 22 '22 09:10

siegi


It's easy to perform with help of Sets (Set does not allow duplicate values)

public List<String> merge(List<List<String>> list) {
    Set<String> uniques = new HashSet<>();
    for(List<String> sublist : list) {
        uniques.addAll(sublist);
    }
    return new ArrayList<>(uniques);
}

p.s. when you want that you merged list will be sorted change HashSet to TreeSet like this: Set<String> uniques = new TreeSet<>();

like image 20
fxrbfg Avatar answered Oct 22 '22 11:10

fxrbfg