Remove duplicates from a List Tag(s): Language


Order is not preserved
import java.util.HashSet;
import java.util.ArrayList;
import java.util.List;

public class ListUtils {

  private ListUtils() {}

  public static <T> void removeDuplicate(List <T> list) {
    HashSet <T> h = new HashSet<T>(list);
    list.clear();
    list.addAll(h);
  }

  public static void main(String[] args) {
    ArrayList<String> list = new ArrayList<String>();
    list.add("Bart");
    list.add("Lisa");
    list.add("Marge");
    list.add("Marge");
    list.add("Barney");
    list.add("Homer");
    list.add("Maggie");
    System.out.println(list);
    // output : [Bart, Lisa, Marge, Marge, Barney, Homer, Maggie]
    ListUtils.removeDuplicate(list);
    System.out.println(list);
    // output : [Bart, Lisa, Maggie, Homer, Barney, Marge]
  }
}
Order is preserved
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class ListUtils {

  private ListUtils() {}

  @SuppressWarnings("unchecked")
  public static <T> void removeDuplicate(List <T> list) {
    Set <T> set = new HashSet <T>();
    List <T> newList = new ArrayList <T>();
    for (Iterator <T>iter = list.iterator();    iter.hasNext(); ) {
       Object element = iter.next();
       if (set.add((T) element))
          newList.add((T) element);
       }
       list.clear();
       list.addAll(newList);
    }

  public static void main(String[] args) {
    ArrayList<String> list = new ArrayList<String>();
    list.add("Bart");
    list.add("Lisa");
    list.add("Marge");
    list.add("Marge");
    list.add("Barney");
    list.add("Homer");
    list.add("Maggie");
    System.out.println(list);
    // output : [Bart, Lisa, Marge, Marge, Barney, Homer, Maggie]
    ListUtils.removeDuplicate(list);
    System.out.println(list);
    // output : [Bart, Lisa, Marge, Barney, Homer, Maggie]
  }
}

blog comments powered by Disqus