What is the default capacity of collection framework classes?

There is no one correct answer here as it will depend on the Java version. For example RFR JDK-7143928 : (coll) Optimize for Empty ArrayList and HashMap made ArrayList and HashMap empty by default in Java 8.

You would have to check the default constructor for each of the mentioned classes in your JDK. In theory this could also vary between JDK build (e.g. Oracle, IBM, Azul...) as default ArrayList capacity is not part of Java Language Specification.


 1. Vector = 10
 2. ArrayList = 10
 3. LinkedList - does not have a capacity
 4. HashMap = 16 (but with the default load factor of 0.75, only 12 can be populated before a resize will happen)
 5. LinkedHashMap = 16 (read above)  
 6. ConcurrentHashMap = 16
 7. HashSet = 16 (it's  based on a HashMap)
 8. LinkedHashSet = 16
 9. TreeSet = does not have one

Just notice that some of them are lazy and all of them are subject to change from release to release.


ArrayList = 10

LinkedList = no intial capacity

HashMap,LinkedHashMap,ConcurrentHashMap,HashSet,LinkedHashSet = 16

TreeSet = empty

Tags:

Java