About Interface Randomaccess

Source: Internet
Author: User

Today I saw java.util.Collections in this tool class.

  Public Static<T>voidFill (list<?)SuperT>list, T obj) {        intSize =list.size (); if(Size < Fill_threshold | | listinstanceofrandomaccess) {//This line             for(inti=0; i<size; i++) List.set (i, obj); } Else{listiterator<?Supert> ITR =List.listiterator ();  for(inti=0; i<size; i++) {itr.next ();            Itr.set (obj); }        }    }


The line identified in the code above, Fill_threshold is 25, that is, if the target list to be filled is not very large, then directly using the above method of efficiency is higher, and note that the list instanceof ramdomaccess this code, By the way, look inside, ramdomaccess interface is an empty interface, the role of an empty interface generally play a role in the identification, such as: Serializable interface.

The documentation in the Raomdomaccess interface describes the general meaning: to identify a list implementation that can provide random access, so that the program using this list can be more efficient when traversing this type of list. That's all.

So, before we go through the list, we can use the IF (list instanceof ramdomaccess) to identify what kind of traversal to use.

Test code:

 PackageCom.zslin.list.demo;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.LinkedList;Importjava.util.List;Importjava.util.RandomAccess;//It is clear from the JDK that in the traversal algorithm for list, especially the list of huge size,//to try to determine whether it belongs to randomaccess (such as ArrayList) or sequence List (such as LinkedList),//because the randomaccess list is suitable for the traversal algorithm, used in the sequence list on the difference is very large, the common practice is://to make a judgment://if (list instance of randomaccess) {//For (int m = 0; m < list.size (); m++) {}//}else{//Iterator iter = List.iterator ();//While (Iter.hasnext ()) {}//    }/** *  * @authorwq<br> *@versioncreated: June 18, 2017 pm 6:01:14<br>*/ Public classtestrandomaccess {//Initialize list     Public Static voidInitlist (List list,intN) { for(inti = 0; I < n; i++) {list.add (i); }    }    //iterating over a list using loops     Public Static voidtraversewithloop (List list) {LongStartTime = 0; LongEndtime = 0; StartTime=System.currenttimemillis ();  for(intCount = 0; Count <= 1000; count++) {             for(inti = 0; I < list.size (); i++) {list.get (i); }} Endtime=System.currenttimemillis (); System.out.println ("Spent with loop iteration" + (Endtime-starttime) + "Ms Time"); }    //iterate over a list using iterators     Public Static voidtraversewithiterator (List list) {LongStartTime = 0; LongEndtime = 0; StartTime=System.currenttimemillis ();  for(intCount = 0; Count <= 1000; count++) {             for(Iterator ITR =list.iterator (); Itr.hasnext ();)            {Itr.next (); }} Endtime=System.currenttimemillis (); System.out.println ("The total cost of using iterator iteration" + (Endtime-starttime) + "Ms Time"); }     Public Static voidTraverse (List list) {LongStartTime = 0; LongEndtime = 0; if(Listinstanceofrandomaccess) {System.out.println ("The list implements the Randomaccess interface"); StartTime=System.currenttimemillis ();  for(intCount = 0; Count <= 1000; count++) {                 for(inti = 0; I < list.size (); i++) {list.get (i); }} Endtime=System.currenttimemillis (); System.out.println ("The iteration spent" + (Endtime-starttime) + "Ms Time"); } Else{System.out.println ("The list does not implement the Randomaccess interface"); StartTime=System.currenttimemillis ();  for(intCount = 0; Count <= 1000; count++) {                 for(Iterator ITR =list.iterator (); Itr.hasnext ();)                {Itr.next (); }} Endtime=System.currenttimemillis (); System.out.println ("The iteration spent" + (Endtime-starttime) + "Ms Time"); }    }     Public Static voidMain (string[] args) {ArrayList ArrayList=NewArrayList (); LinkedList LinkedList=NewLinkedList (); Initlist (ArrayList,1000); Initlist (LinkedList,1000);        Traverse (ArrayList);        Traverse (LinkedList);        Traversewithiterator (ArrayList);        Traversewithloop (ArrayList);        Traversewithiterator (LinkedList);    Traversewithloop (LinkedList); }}

The results of the operation are as follows:

The list implements the Randomaccess interface iteration took a total of 9ms time the list did not implement the Randomaccess interface iteration took a total of 10ms time to use the iterator iteration altogether spent 12ms time using the loop iteration spent a total of 7ms time using iterator iteration altogether spent 14ms time it took 396ms time to use the loop iteration

Above.

About Interface Randomaccess

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.