1. 程式人生 > 其它 >.Net Core AutoFac 使用方法講解大全,具體詳細使用知識總結

.Net Core AutoFac 使用方法講解大全,具體詳細使用知識總結

import java.util.*;
import java.util.stream.Collectors;

public class QuChong {
public static void main(String[] args) {

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(1);
list.add(3);
list.add(1);
list.add(12);
list.add(1);
list.add(3);
list.add(3);


removeDuplicate(list);
List a = removeDuplicateFromJava8(list);
removeDuplicateByContains(list);
List a = removeDuplicateHashSet(list);
System.out.println( "result:"+ a);
}

//1.Java8 新特性
public static List removeDuplicateFromJava8(List list){

List list1=(List) list.stream().distinct().collect(Collectors.toList());
return list1;

}

//2.迴圈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);
}
}
}
System.out.println("removeDuplicate" + list);
return list;
}
//把list裡的物件遍歷一遍,用list.contains(),如果不存在就放入到另一個list集合中
public static List removeDuplicateByContains(List list){
List listTemp = new ArrayList();
for(int i=0;i<list.size();i++){
if(!listTemp.contains(list.get(i))){
listTemp.add(list.get(i));
}
}
System.out.println("removeDuplicateByContains" + listTemp);
return listTemp;

}

// 通過HashSet踢除重複元素
public static List removeDuplicateHashSet(List list) {
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
return list;
}



}