ArrayList collection implementations RandomAccess interface What role? Why did not implement this set of interfaces LinkedList

See: https://blog.csdn.net/weixin_39148512/article/details/79234817

It is well known in the List collection, we often use the ArrayList and LinkedList collections, but by looking at the source code, you will find ArrayList achieve RandomAccess interface, but the RandomAccess interface which is empty! Linked did not realize RandomAccess interface.

Why is this?

-----------------------------------------------------------------------------------------------------

This is achieved RandomAccess ArrayList interface source code

 

 

------------------------------------------------------------------------------------------------------

This is LinkedList source, did not achieve the RandomAccess interface

 

 

------------------------------------------------------------------------------------------------------

This is a source code interface RandomAccess

 

The original RandomAccess interface is a marker interfaces (Marker), but to achieve this interface has what effect?

Answer: As long as List collection implement this interface can support fast random access, but someone asked, fast random access is what? what's the effect?

By looking at the Collections class binarySearch () method, the following source code:

 

 

It can be seen, it is determined whether the list RandomAccess implement interface implemented indexedBinarySerach (list, key) or iteratorBinarySerach (list, key) method. ps (instanceof whose role is to determine whether an object of a class or interface type)

 

Well, some people doubt that the implementation of these two methods What is the difference?

View the next indexedBinarySerach (list, key) method Source:

 

 

-------------------------------------------------------------------------------------------------------------

 

View the next iteratorBinarySerach (list, key) method Source:

 

 

By viewing the source code to implement the RandomAccess found List collection using the general interface for loop traversal, without which the interface is implemented using an iterator.

Next, we will test the next ArrayList and LinkedList using these two methods is how their performance!

for loop through ArrayList

-------------------------------------------------------------------------------------------------------------

iterator iterates over ArrayList

for loop iterates LinkedList

 

 

-------------------------------------------------------------------------------------------------------------

iterator iterates over LinkedList

 


-------------------------------------------------- -------------------------------------------------- ---------
operating results:

 

As can be seen from the above data, ArrayList for loop traversal traverses faster than iterator iterator, LinkedList using iterator iterates over faster than for looping through,

So, when we do the project, should take into account the different subclasses List collection of different traversal, can improve performance!

However, it was issued a doubt, that determine how a subclass of List received ArrayList or LinkedList is it?

Then you need to use instanceof to determine whether to implement a subclass of List collection RandomAccess interfaces!

main methods:

 

operation result:

 

Summary: RandomAccess Interface basket case the presence is set to be able to better determine whether the LinkedList ArrayList Alternatively, it is possible to select a better way to navigate better, to improve performance!

Guess you like

Origin www.cnblogs.com/lukelook/p/11284945.html