I know that the default capacity of Vector
class in java is 10 and similarly ArrayList
also have it's default capacity 10. But what is the default capacity of the following classes:
Or is there any other way to get the default capacity of all collection framework classes in java?
24) What is the default size of load factor in hashing based collection? The default size of load factor is 0.75. The default capacity is computed as initial capacity * load factor. For example, 16 * 0.75 = 12.
Whenever an instance of ArrayList in Java is created then by default the capacity of Arraylist is 10.
Constructs a new, empty hashtable with a default initial capacity (11) and load factor (0.75).
Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With