1, first remove (int index) must be faster than remove (object o) fast, the index must be fast, no doubt, of course, know the premise of the index
2, if: have list_1 set 140,000 data, List_2 set 70,000
Code 1:
List<integer> List1 = new arraylist<> ();
list<integer> list2 = new arraylist<> ();
for (int i = 0;i<14000;i++) {
list1.add (i);
if (i%2==0) {
list2.add (i);
}
}
SYSTEM.OUT.PRINTLN ("Size:" +list1.size ());
Long start = System.currenttimemillis ();
for (Integer i2:list2) {
list1.remove (i2);
Here's a small question: The parameter type of remove (I2) is (object o) or (int index);
If so: Remove (2), and what type of parameter.
tested: The former is object and the latter is int. It's not surprising.
}
Long end = System.currenttimemillis ();
System.out.println ("Time:" + (End-start) + "ms\n" + "Size:" +list1.size ());
Print results:
size:14000
Time:44ms
size:7000
If the number is multiplied by 10, which is 14000*10
140000
Time:4229ms
size:70000
Description: Time consumption is almost 10*10 times, because there are 1 loops inside the Remove (object o), plus the outer list2 foreach, there are 2 loops,
Description time complexity is O (n^2),
Code 2:
List<integer> List1 = new arraylist<> ();
list<integer> list2 = new arraylist<> ();
for (int i = 0;i<14000;i++) {
list1.add (i);
if (i%2==0) {
list2.add (i);
}
}
System.out.println (List1.size ());
Long start = System.currenttimemillis ();
List1.removeall (LIST2);
Long end = System.currenttimemillis ();
System.out.println ("Time:" + (End-start) + "ms\n" + "Size:" +list1.size ());
Print results:
size:14000
Time:91ms
size:7000
If the number is multiplied by 10, which is 14000*10
size:140000
Time:8820ms
size:70000
Description: Time consumption is almost 10*10 times, because RemoveAll () nested 2 for loops, List1 loops and list2, indicating time complexity is also O (n^2)
Summary of code 1 and Code 2: All are removed from the List1 List2, and code 1 takes less time than code 2,
This means that the remove (object o) is more efficient than RemoveAll ()