01, use two for loops to deduplicate List (ordered)
** @param List ** / public static List removeDuplicationBy2For(List<Integer> List) {for (int) i=0; i<list.size(); i++) { for (int j=i+1; j<list.size(); j++) { if(list.get(i).equals(list.get(j))){ list.remove(j); } } } return list; }Copy the code
02, use the List contains method to loop through (ordered)
/ * * use the contains method to iterate over the List collection (orderly) * * * * @ param List/public static List removeDuplicationByContains (List < Integer > List) { List<Integer> newList =new ArrayList<>(); for (int i=0; i<list.size(); i++) { boolean isContains =newList.contains(list.get(i)); if(! isContains){ newList.add(list.get(i)); } } list.clear(); list.addAll(newList); return list; }Copy the code
Use HashSet to deduplicate List (unordered)
/ * * use HashSet implement List to heavy (disorder) * * * * @ param List/public static List removeDuplicationByHashSet (List < Integer > List) { HashSet set = new HashSet(list); Clear (); // Clear (); // Add the HashSet object to the List set.addall (set); return list; }Copy the code
04、使用TreeSet实现List去重(有序)
/ * * use TreeSet implement List to heavy (orderly) * * * * @ param List/public static List removeDuplicationByTreeSet (List < Integer > List) { TreeSet set = new TreeSet(list); Clear (); // Clear (); // Add the HashSet object to the List set.addall (set); return list; }Copy the code
5. Stream List deduplicate (ordered)
/ * * use java8 new features stream implementation List to heavy (orderly) * * * * @ param List/public static List removeDuplicationByStream (List < Integer > List) { List newList = list.stream().distinct().collect(Collectors.toList()); return newList; }Copy the code
Efficiency test code
public static void main(String args[]) { List<Integer> list1 = new ArrayList<>(); List<Integer> list2 = new ArrayList<>(); List<Integer> list3 = new ArrayList<>(); List<Integer> list4 = new ArrayList<>(); List<Integer> list5 = new ArrayList<>(); Random random =new Random(); for (int i = 0; i < 100000; i++) { int value =random.nextInt(500); list1.add(value); list2.add(value); list3.add(value); list4.add(value); list5.add(value); } long startTime ; long endTime; startTime = System.currentTimeMillis(); removeDuplicationByHashSet(list1); endTime = System.currentTimeMillis(); System.out.println(" Use HashSet to implement List deduplicate time :"+(endtime-startTime)+" milliseconds "); startTime = System.currentTimeMillis(); removeDuplicationByTreeSet(list2); endTime = System.currentTimeMillis(); System.out.println(" TreeSet :"+(endtime-starttime)+" milliseconds "); startTime = System.currentTimeMillis(); removeDuplicationByStream(list3); endTime = System.currentTimeMillis(); System.out.println(" Use stream to deduplicate List :"+(endtime-starttime)+" milliseconds "); startTime = System.currentTimeMillis(); removeDuplicationBy2For(list4); endTime = System.currentTimeMillis(); System.out.println(" Use two for loops to deduplicate the List :"+(endtime-starttime)+" milliseconds "); startTime = System.currentTimeMillis(); removeDuplicationByContains(list5); endTime = System.currentTimeMillis(); System.out.println(" use List contains method to loop through :"+(endtime-starttime)+" milliseconds "); }Copy the code
Results:
Use HashSet to achieve List reduplication time :40 ms
Use TreeSet to implement List derepeatable time :36 ms
Use stream, a new java8 feature, to de-duplicate the List :78 milliseconds
List deduplications are implemented using two for loops :533 milliseconds
Loop through using the List collection contains method :40 milliseconds
More test results
Random number in the range of 100:
Use HashSet to achieve List derepeatable time :32 ms
Use TreeSet to implement List reduplication time :40 ms
Use a new Java8 feature stream to de-duplicate the List: 128ms
Use two for loops to de-duplicate the List :693 milliseconds
Loop through using the List collection contains method :30 milliseconds
Random number in the range of 1000:
Use HashSet to achieve List reduplication time :34 milliseconds
Use TreeSet to implement List reduplication time :72 milliseconds
Use a new Java8 feature stream to de-duplicate the List :125 ms
List deduplications are implemented using two for loops :1063 milliseconds
Loop through using the List collection contains method :85 milliseconds
Random number in the range of 10000:
Use HashSet to achieve List derepeatable time :51 ms
TreeSet: 103ms
Use stream, a new java8 feature, to de-duplicate the List :201 milliseconds
Use two for loops to de-duplicate the List :5448 milliseconds
Loop through using the List collection contains method :791 milliseconds
conclusion
Unordered HashSet, ordered TreeSet
Java has a wide range of knowledge, and the interview questions involve a wide range of topics, including: Java basic, Java concurrent, JVM, MySQL, data structure, algorithm, Spring, microservices, MQ, and so on, involving knowledge how huge, so we often do not know how to start when reviewing, today xiaobian to bring you a set of Java interview questions, question library is very comprehensive, Including Java foundation, Java collection, JVM, Java concurrency, Spring family bucket, Redis, MySQL, Dubbo, Netty, MQ and so on, including Java back-end knowledge 2000 +, part as follows:
Access to information: pay attention to the public number: “programmer Bai Nannan” access to the above information
Important things say three times, forward + forward + forward, must remember to forward oh!!