Collection Frameworks Cheatsheet

#Cheatsheet

Interface and Class Hierarchy

I = Interface AC = Abstract Class Underlying Data Structure mentioned beside the classes.

💡 Every Collection class is based on some standard data structure.

In order of hierarchy:

Important Differences

Collections Class vs Collection Interface

Comparison with Arrays

data.structure.primary.array (Private)ArrayList
DefinitionAn array is a dynamically-created object. It serves as a container that holds the constant number of values of the same type. It has a contiguous memory location.The ArrayList is a class of Java Collections framework.
SizeStatic - fixed-length data structureDynamic - variable length data structure - can resize itself when needed
Size specificationMandatory to provide size while initializingSize can be left unspecified - default initial capacity will be used
PerformanceFixed size => FasterInternally backed by array. Resize operation slows down the performance
Primitive/generic typeCan store both primitives and objectsCan't store primitives - automatically converts them to objects. For example, int is converted to Integer.
Getting the lengtharr.lengtharr.size()
Adding elementsCan add elements using assignment operatoradd() function
Multiple dimensionsCan be multi-dimensionalCAN'T be multi-dimensional

HashMap vs Hashtable

HashMapHashtable
Introduced in1.21.0
Thread SafetyNot thread safe (not synchronized) => works with single threadThread safe (synchronized) => works with multiple threads
SpeedFasterSlower
Null keyOne null key allowedNull key not allowed

ArrayList and Vector's only Difference

Vector is thread-safe (most methods are synchronised) ArrayList is not.

💡 If something is thread-safe, it will inevitably have a lower performance in comparison to a corresponding non-synchronised something.

size() vs capacity()

  • size = number of objects currently
  • capacity = number of objects which can be accommodated