简体   繁体   中英

How expensive is calling size() on List or Map in Java?

How expensive is calling size() on List or Map in Java? or it is better to save size()'s value in a variable if accessed frequently?

The answer is that it depends on the actual implementation class. For some Map and Collection classes, size() is a cheap constant-time operation. For others, it may entail counting the members.

The Java Collections Cheatsheet (V2) is normally a good source for this kind of information, but the host server is currently a bit sick.

The "coderfriendly.com" domain is no more, but I tracked down a copy of the cheat-sheet on scribd.com.

The cost of size() will also be obvious from looking at the source code. (And this is an "implementation detail" that is pretty much guaranteed to not change ... for the standard collection classes.)

FOLLOWUP

Unfortunately, the cheatsheet only documents the complexity of size for queue implementations. I think that's because it is O(1) for all other collections; see @seanizer's answer.

for ArrayList the implementation is like

   public int size() {
       return lastIndex - firstIndex;
   }

So not over head

You can check the source code for detailed info for your required Impl .

Note: The source given is from openjdk

List and Map are interfaces, so it's impossible to say . For the implementations in the Java Standard API, the size is generally kept in a field and thus not performance-relevant.

For most Collections, calling size() is a constant-time operation. There are however some exceptions. One is ConcurrentLinkedQueue . From the Javadoc of the size() method :

Beware that, unlike in most collections, this method is NOT a constant-time operation. Because of the asynchronous nature of these queues, determining the current number of elements requires an O(n) traversal.

So I'm afraid there's no generic answer, you have to check the documentation of the individual collection you are using.

Implement it, then test it. If it is slow, take a closer look.

"Premature optimisation is the root of all evil." - D. Knuth

Also: You should not require certain implementation features, especially if they are black-boxed. What happens if you replace that list with a concurrent list at a later date? What happens if Oracle decides to rewrite List? Will it still be fast? You just don't know.

You don't have to worry much about that. The list implementations keep track of size. The cost of the call is just O(1). If you are very curious, you can read the source code for the implementations of Collection's concrete classes and see the size() method there.

实现从私有预先计算的变量中获取它,因此它并不昂贵。

不需要存储。它根本不贵。检查ArrayListHashMap的来源。

I think some implementations of LinkedList count the total for each call. The call to a method itself can be a little taxing, but only if we're talking about large iterations or driver coding for hardware would that really be an issue.

In either case, if you save it to a local variable, there won't be any problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM