Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicates from List using Guava

How can we remove duplicates from List with the help of Guava api?

Currently I am following this:

private List<T> removeDuplicate(List<T> list){ return new ArrayList<T>(new LinkedHashSet<T>(list)); } 
like image 332
Priyank Doshi Avatar asked Sep 03 '12 04:09

Priyank Doshi


People also ask

How HashSet remove duplicates from a list?

The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList: List<String> al = new ArrayList<>(); // add elements to al, including duplicates Set<String> hs = new HashSet<>(); hs. addAll(al); al. clear(); al.


2 Answers

Probably the most efficient way is ImmutableSet.copyOf(list).asList(), which eliminates duplicates and preserves iteration order.

(But your implementation with LinkedHashSet would be nearly as efficient, and wouldn't throw up on nulls, in the unlikely event you actually wanted nulls in your collection.)

like image 136
Louis Wasserman Avatar answered Sep 24 '22 10:09

Louis Wasserman


I love Louis' answer for the simplicity of it (and because it's the only answer that doesn't require 2 full iterations), but unfortunately in the real world, you often encounter situations where null does occur. Here's a slightly longer null-safe version:

ImmutableSet.copyOf(     Iterables.filter(         list, Predicates.not(Predicates.isNull()))).asList(); 

Or, with static imports:

copyOf(filter(list, not(isNull()))).asList(); 

Of course you need to be aware of the fact that all null Values will be lost from the List.

like image 31
Sean Patrick Floyd Avatar answered Sep 22 '22 10:09

Sean Patrick Floyd