通过HashSet删除
public static void removeDuplicate(List list) { HashSet h = new HashSet(list); list.clear(); list.addAll(h); System.out.println(list); }删除重复元素,并保持顺序
public static void removeDuplicateWithOrder(List list) { Set set = new HashSet(); List newList = new ArrayList(); for (Iterator iter = list.iterator(); iter.hasNext();) { Object element = iter.next(); if (set.add(element)) newList.add(element); } list.clear(); list.addAll(newList); System.out.println( " remove duplicate " + list); } 获取两个集合中相同的元素public List<String> void getDupliteElements(List<String> list1, List<String> list2){
List<String> mList = new ArrayList<>;
for(String str : list1){
if(list2.contains(str)){
mList.add(str);
}
}
}