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));
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Probably the most efficient way is
ImmutableSet.copyOf(list).asList(), which eliminates duplicates and preserves iteration order.(But your implementation with
LinkedHashSetwould be nearly as efficient, and wouldn’t throw up on nulls, in the unlikely event you actually wanted nulls in your collection.)