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 ArrayList
s. 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
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.
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.
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<>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With