List多种去重方法&去重后排序
2019-05-20 本文已影响0人
可乐爱吃薯片
通过HashSet踢除重复元素
public static List removeDuplicate(List list) {
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
return list;
}
循环List中的所有元素然后删除重复
public static List removeDuplicate(List list) {
for ( int i = 0 ; i < list.size() - 1 ; i ++ ) {
for ( int j = list.size() - 1 ; j > i; j -- ) {
if (list.get(j).equals(list.get(i))) {
list.remove(j);
}
}
}
return list;
}
List去重后排序
public static void pastLeep5(List<String> list){
System.out.println("list = [" + list + "]");
//new TreeSet<String>(list);去重后排序
List<String> listNew=new ArrayList<>(new TreeSet<String>(list));
System.out.println("listNew = [" + listNew + "]");
}