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