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)); }
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.
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.)
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.
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