1. 程式人生 > >List去掉重複記錄,保持順序

List去掉重複記錄,保持順序

public static List<String> removeDuplicateWithOrder(List<String> list) {
     Set<String> set = new HashSet<String>();
      List<String> newList = new ArrayList<String>();
    for (Iterator<String> iter = list.iterator(); iter.hasNext();) {
          String element = iter.next();
          if (set.add(element))
             newList.add(element);
       }
      list.clear();
      list.addAll(newList);
      return list;
}