ArrayList method: Comparison of time complexity of remove (object o), remove (int index), RemoveAll (Collection c)

Source: Internet
Author: User
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 ()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.