Java data structures. SOURCE Read __ Data structure

Source: Internet
Author: User




In the Collections tool class.


Collections.synchronizedlist


public static <T> list<t> synchronizedlist (list<t> List) {
Return (list instanceof randomaccess?)
New Synchronizedrandomaccesslist<t> (list):
New synchronizedlist<t> (list));
}

# #仅仅是通过判断是否实现RandomAccess接口, and return different synchronizedlist (inner) classes,

# #RandomAccess foreach faster, iterator slower, the difference is not obvious (such as i5, I test, a arraylist,90w object, foreach out is more than 6 MS, iterator out 7ms)

# #SequenceAccess foreach is slower and iterator faster. The difference is very clear. (such as i5, I test, a linkedlist,10w object, foreach out is more than 200 ms, iterator out only 4ms)



LinkedList the Get (n) method.    The source code is also a little bit good, one does not pay attention to really is a pit. (This is the reason Sequenceaccess's foreach is slow)

Private entry<e> Entry (int index) {
if (Index < 0 | | | index >= size)
throw new Indexoutofboundsexception ("Index:" +index+
", Size:" +size);
entry<e> E = header;
if (Index < (size >> 1)) {
for (int i = 0; I <= index; i++)
e = E.next;
} else {
for (int i = size; i > index; i--)
e = e.previous;
}
return e;
}






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.