Comparison of ArrayList and rule list, rule list

Source: Internet
Author: User

Comparison of ArrayList and rule list, rule list

 

 

 

Comparison 1: add content

 

Method involved: add

 

Public void add_test () {List <Person> addlist = new ArrayList <Person> (); Person p = new Person ("dumb", 20 );
Long begin = System. currentTimeMillis (); System. out. println ("ArrayList start time:" + begin); for (int I = 0; I <10000000; I ++) {addlist. add (p); // System. out. println ("Number" + I + "+" time used: "+ (System. currentTimeMillis ()-begin);} long over = System. currentTimeMillis (); System. out. println ("ArrayList end time:" + over); System. out. println ("ArrayList final time:" + (over-begin ));}

Note: This is the test code of the ArrayList, but you only need to change the addlist type to the addlist list.

Result:

 

ArrayList

 

 

Shortlist

 

Apparently, it takes an ArrayList <partition list

 

 

 

Comparison 2: Get content

Involved Methods: get, contains, indexOf, lastIndexOf

 

1. get Test

Public void get_test () {// preparations, add a bunch of projects, and add a silly List <Person> getlist = newArrayList<Person> (); Person p = new Person ("dumb", 20); for (int I = 0; I <10000000; I ++) {getlist. add (p);} Person p2 = new Person ("silly", 20); getlist. add (77777, p2); System. out. println ("added successfully"); // get test long begin = System. currentTimeMillis (); System. out. println ("get Test start time:" + begin); getlist. get (7777777); long over = System. currentTimeMillis (); System. out. println ("get test end time:" + over); System. out. println ("get test end time:" + (over-begin ));}

Result

ArrayList

Shortlist

 

 

 

2. contains Test

// Contains test long begin = System. currentTimeMillis (); for (int I = 0; I <10000; I ++) {getlist. contains (p2);} long over = System. currentTimeMillis (); System. out. println ("contains test end time:" + (over-begin ));

Note: you only need to replace the code in the above test with this section, you can test contains, and the preparation code remains unchanged.

Result

ArrayList

Shortlist

 

 

 

3. indexOf Test

// IndexOf test long begin = System. currentTimeMillis (); for (int I = 0; I <10000; I ++) {getlist. indexOf (p2);} long over = System. currentTimeMillis (); System. out. println ("indexOf test end time:" + (over-begin ));

Result

ArrayList

Shortlist

 

 

 

4. lastIndexOf Test

// LastIndexOf test long begin = System. currentTimeMillis (); for (int I = 0; I <100; I ++) {getlist. lastIndexOf (p2);} long over = System. currentTimeMillis (); System. out. println ("lastIndexOf test end time:" + (over-begin ));

Result

ArrayList

Shortlist

 

 

Summary:

Get: ArrayList <rule list, which varies greatly.

Contains: ArrayList <distinct list, the difference is not that big, the following two are also

IndexOf: ArrayList <rule list

LastIndexOf: ArrayList <rule list

 

 

 

Comparison 3: delete content

 

Involved Methods: remove (based on index) and remove (Based on Object)

 

 

1. remove (based on the index)

Public void remove_test () {// preparations, add a bunch of dumb List <Person> getlist = newArrayList<Person> (); Person p = new Person ("dumb", 20); for (int I = 0; I <10000000; I ++) {getlist. add (p);} System. out. println ("added successfully"); // remove (int index) test System. out. println (getlist. size (); long begin = System. currentTimeMillis (); for (int I = 0; I <10000000; I ++) {getlist.Remove(Getlist. size ()-1);} long over = System. currentTimeMillis (); System. out. println ("remove (int index) test end time:" + (over-begin); System. out. println (getlist. size ());
}

Result

ArrayList

Shortlist

 

 

 

2. remove (based on the object)

// Remove (Object o) test System. out. println (getlist. size (); long begin = System. currentTimeMillis (); for (int I = 0; I <999; I ++) {getlist.Remove(P);} long over = System. currentTimeMillis (); System. out. println ("remove (Object o) test end time:" + (over-begin); System. out. println (getlist. size ());

Result

ArrayList

Shortlist

 

 

 

Summary:

Remove (based on the index): ArrayList <distinct list, the difference is not big

Remove (Based on Object): ArrayList> revoke list, which varies greatly.

 

 

 

Further

 

When inserting content: Specify the insert position based on whether or not

ArrayList

 

 

Shortlist

 

 

Depending on the Insert Location

ArrayList

 

Shortlist

 

 

 

When obtaining the content, the index is located at the front, middle, and back of the entire set.

 

ArrayList

 

Shortlist

 

 

When the content is removed, the index is located at the front, middle, and back of the entire set.

 

 

ArrayList

 

Shortlist

 

 

 

Summary:

When inserting content:

Based on whether the Insert Location is specified or not,

ArrayList: Slow Indexing

Shortlist: similar

Depending on the Insert Location

ArrayList: the longer the position, the slower it is.

Slow list: the fastest in the front, the slower in the back, and the slowest in the middle

 

When obtaining the content,

Based on the index location in the front, middle, and back of the entire set

ArrayList: similar

Slow list: the index is slower.

 

When removing content,

Based on the index location in the front, middle, and back of the entire set

ArrayList: similar

Slow list: the index is slower.

 

 

 

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.