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;
}