• 周六. 10 月 5th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Java collection class: randomaccess interface for ‘random access’

King Wang

1 月 3, 2022

extraction RandomAccess Interface

If we use Java For development , One of the most commonly used containers is List Set up , and List The most commonly used set is ArrayList and LinkedList Two classes , The two are often used for comparison . Because I’m studying recently Java Collection class source code , Naturally, we can’t let go of these two categories , So , Look at their source code , I find ,ArrayList To achieve one called RandomAccess The interface of , and LinkedList There is no ,

public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable

After opening the source code , There is nothing in the interface , This is an empty interface , And is 1.4 It was introduced

 * @since 1.4
*/
public interface RandomAccess {
}

So what does this interface do ?

Flag interface

Through the official website API, I knew that. , It turns out that this is a flag interface , Here is the original text of the official website :

public interface RandomAccess

Marker interface used by List implementations to indicate that they support fast (generally constant time) random access.

The general meaning of this passage is to say RandomAccess It’s a logo interface , Indicates the implementation of this interface List Collections support fast random access . in other words , The collection that implements this interface supports Fast random access Strategic .

meanwhile , The official website also specifically states , If this interface is implemented List, So use for Getting data in a loop is better than using iterators .

As a rule of thumb, aList implementation should implement this interface if, for typical instances of the class, this loop:

​ for (int i=0, n=list.size(); i < n; i++)
list.get(i);

​ for (Iterator i=list.iterator(); i.hasNext(); )
i.next();

Let’s do a test , With ArrayList For example .

Demo test

Create two methods , One use for loop get() The way data traverses the collection , The other is using iterators , The time taken to return separately :

public static long arrayFor() {
List<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 100000; i++) {
list.add(i);
}
// Starting time
long startTime = System.currentTimeMillis();
for (int j = 0; j < list.size(); j++) {
Object num = list.get(j);
}
// End time
long endTime = System.currentTimeMillis();
// Return time
return endTime-startTime;
}
public static long arrayIterator() {
List<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 100000; i++) {
list.add(i);
}
long startTime = System.currentTimeMillis();
Iterator iterator = list.iterator();
while (iterator.hasNext()){
Object next = iterator.next();
}
long endTime = System.currentTimeMillis();
return endTime-startTime;
}

next , stay mian Test in method :

public static void main(String[] args) {
long time1 = arrayFor();
long time2 = arrayIterator();
System.out.println("ArrayList for The time taken for the cycle =="+time1);
System.out.println("ArrayList The time taken by the iterator =="+time2);
}

Run the program , Output results

ArrayList for The time taken for the cycle ==2
ArrayList The time taken by the iterator ==3

It can be seen that ,for Loop traversal element time is less than iterator , prove RandomAccess Interfaces do have this effect .

Of course , Today’s machine languages are so high , The performance gap between the two methods is negligible , Especially when the amount of data is small . therefore , I think , There is no need to over pursue which way is good in daily use , Just follow your own habits .

发表回复