3. 2016.10.15 Java: Remove Repeated Element in List

  • http://stackoverflow.com/questions/203984/how-do-i-remove-repeated-elements-from-arraylist
  • http://stackoverflow.com/questions/13259535/how-to-maintain-a-unique-list-in-java
    import java.util.HashSet;
    import java.util.Set;
    List<String> al = new ArrayList<>();
    
    // add elements to al, including duplicates
    Set<String> hs = new HashSet<>();
    hs.addAll(al);
    al.clear();
    al.addAll(hs);