Sitemap
Ece Aslan

Ece Aslan

Java Collections

6 min readApr 15, 2026

--

JCF (Java Collection Framework) is a set of classes and interfaces that provide ready-premade data structures for us to use. They are useful when we want to store more than one object that are related together in one place.

There are interfaces such as List, Set, Map, Queue, ArrayList, HashSet, HashMap, PriorityQueue. These interfaces support dynamic resizing, includes algorithms, improves code reusability.

  1. Core Interfaces are as follows:

1. Collection Interface:

  • Extends Iterable, meaning collections can be traversed.
  • Represents a group of objects as a single unit.
  • Dynamic resizing.
  • Provides simple methods like add or remove.
public interface Collection <E> extends Iterable<E>

— — — — — — — — — — — — — — — — —

  • Example: Creating a ArrayList with the type of String as a Collection.
Collection<String> fruits = new ArrayList<>();

In Java, we create an object of the ArrayList class which implements the interface and we assign this to interface reference. There isn’t a way of creating an object right out of an interface.

Sub Interfaces of Collection Interface:

a. List Interface:

  • Ordered sequence.
  • Allows duplicates.
  • Extends Collection Interface.
  • Implements ArrayList, LinkedList, Stack, Vector.
  • Supports multiple nulls, bidirectional traversal.
  • add(), set(), indexOf(), lastIndexOf(), remove(), get(), contains(), size(), clear(), isEmpty(), equals()…

Implementations of List Interface:

i. ArrayList Class:

  • Access by indexes to elements.
  • Cannot store primitive types, so Wrapper classes are used instead.
  • Has a default capacity of 10, when needed for resizing it increases its size by %50 every time.
  • Not thread-safe.

ii. LinkedList Class:

  • Better performance than ArrayList when insertion/deletion operations are applied.
  • Cannot access elements by indexes. Nodes are connected to each other by their references as previous and next nodes. So, accessing must be done by traversing from head.
  • Implements both List and Deque interfaces from Collection which makes itself ideal for FIFO and double-ended queue operations.
  • Uses double-linked list logic.
  • Slower random access than ArrayList.
  • Uses more memory, extra node pointers on every node.
  • Iteration is slower but insertion/deletion is faster.
  • Not thread-safe.

iii. Vector Class:

  • Thread-safe. Meaning it can be reached/invoked by multiple threads without causing any damage to the data. Meaning that there will be a queue of threads to work on one Vector. This prevents data corruption, crashing.
  • Implements List, RandomAccess, Cloneable, Serializable interfaces.
  • It’s one of the legacy classes in Java as it’s one of the early introduced ones.
  • If multi-threading is not needed, ArrayList is actually chosen over Vector.

iv. Stack Class:

  • Extends Vector class.
  • Thread-safe.
  • Implements List, RandomAccess, Cloneable, Serializable.
  • Also, a legacy class. In modern Java, ArrayDeque and LinkedList are preferred. If the situation is single threaded, then ArrayDeque is preferred also.
  • Instead of add(), get(), remove(); Stack has push(), peek(), pop(). But still uses other methods as itself extends Vector class.

— — — — — — — — — — — — —

b. Set Interface:

  • No duplicates.
  • Except TreeSet, can contain only one null value. TreeSet doesn’t allow any null value.
  • Provides search, insertion, deletion operations. add(), contains(), remove().
  • Implementations for HashSet, EnumSet, TreeSet and LinkedHashSet.
public interface Set<E> extends Collection <E>
  • But Set is also an interface which means that we cannot create an object directly from it. So, we use a class that implements it.
Set<Obj> set = new HashSet<Obj>();

Implementations of Set Interface:

Get Ece Aslan’s stories in your inbox

Join Medium for free to get updates from this writer.

i. HashSet Class:

  • Uses HashMap which is a hash table data structure.
  • Implements Serializable and Cloneable.
  • Not thread-safe.
  • Uses wrapper classes.
  • Default capacity is 16 and load factor is considered 0,75. oldCapacity * loadFactor is the way to calculate for threshold. If threshold is exceeded, the capacity is doubled.

ii. EnumSet Class:

  • In enum types, EnumSet is slightly faster in comparison.
  • Does not allow null values. If tried, NullPointerException is thrown.
  • Extends Serializable and implements Cloneable, AbstractSet which implements Set.
  • Since it’s an abstract class, a direct instance cannot be created from it.
  • Less than or equal to 64 number of elements is used RegularEnumSet and if it’s more than 64, then, JumboEnumSet is used.
  • Only enum constants from a single enum type can be used.
  • .allOf(), .noneOf(), .range(), .of() are the methods when we create an EnumSet.

iii. LinkedHashSet Class:

  • Maintains the insertion order even though it extends HashSet which does not maintain it.
  • Fast performance for basic operations.
  • Extends HashSet.
  • Uses LinkedList and HashMap as a data structure.

iv. TreeSet Class:

  • Does not allow null values unlike other set types. If we try to add a null value, NullPointerException will be thrown.
  • Implements NavigableSet interface (extends SortedSet extends Set) which provides navigation methods such as lower(), higher() and etc.
  • Not thread-safe.
  • When added new element, it’s added by ascending order. Duplicates are ignored.
  • Access is with contains(), first(), last() methods.

— — — — — — — — — — — — —

c. SortedSet Interface:

  • Extends Set, implementation for TreeSet to create an object from it.
  • No duplicates. No nulls.
  • As it's called, element order is by ascending.

— — — — — — — — — — — — —

d. NavigableSet Interface

  • Extends SortedSet, provides navigation methods to retrieve elements closest to the given element.
  • Elements are in order of ascending.
  • As it’s an interface, no way of creating direct objects from it.
  • Usually implemented by TreeSet class.

— — — — — — — — — — — — —

e. Queue Interface

  • FIFO is followed in LinkedList and priority order in PriorityQueue.
  • No access by indexes.
  • Duplicates are allowed.
  • Implementations are LinkedList, ArrayDeque, PriorityQueue, ConcurrentLinkedQueue, BlockingQueue.
  • Has basic methods like add(), remove() etc.

i. LinkedList has been explained above.

ii. ArrayDeque

iii. PriorityQueue:

  • Standard queue operations.
  • Dynamic resizing.
  • No nulls.
  • Heap data structure is used for priority order and efficient insertion/deletion operations.
  • Default implementation for PriorityQueue is min-heap.

iv. ConcurrentLinkedQueue

v. BlockingQueue Interface

f. Deque Interface

  • Can be used as a stack/LIFO or a queue/FIFO.
  • Stands for double-ended queue.
  • Dynamic resizing.
  • Thread-safe.
  • Extends Queue.
  • Implementations for;

i. ArrayDeque
ii. LinkedList
iii. ConcurrentLinkedDeque

2. Map Interface

  • A collection of key-value pairs, keys are unique, but values can be duplicated.
  • Efficient retrieval, insertion and deletion operations by keys.
  • HashMap and LinkedHashMap allow only one null key, but TreeMap does not allow any.
  • ConcurrentHashMap is used for when thread-safe operations are needed.
  • Extend for Hashtable class and SortedMap interface.
  • Implementation for HashMap class.

i. HashMap

  • Not thread-safe.
  • Hashing is used internally.
  • For insertion order, LinkedHashMap is preferred while for sorted order TreeMap is used.
  • Allows one null key but multiple null values. If there is a second null key, its value will be overwritten.
  • Fast and efficient key-based retrieval insertion and removal operations. O(1) time efficient to be precise.

ii. LinkedHashMap

  • Insertion order.
  • One null key and multiple null values are allowed.
  • Not thread-safe.
  • Since it’s extending HashMap, there will be key-value pairs. And since it’s also Linked and maintains the insertion order which means there will be references to the previous and the next nodes.

iii. TreeMap

  • Uses Red-Black Tree internally for efficient sorting. Provides O(log n) time for insertion/deletion operations.
  • No null keys. But there can be null values.
  • Implements NavigableMap which extends SortedMap which extends Map Interface itself.
  • Has a custom operator, if not natural order is used.

iv. Hashtable

  • No null keys or null values.
  • Uses hashing to retrieve elements.
  • Thread-safe. Which makes it slower than HashMap.

3. Enumeration Interface

  • Legacy cursor used before Iterator and ListIterator.
  • Traversal is possible only forward.
  • Preferred with Vector, Stack and Hashtable.

— — — — — — — — — — — — — — — — — — — —

Here’s two pictures to visualize the relationship between interfaces and classes.

Press enter or click to view image in full size
Collection Interface Hierarchy from https://media.geeksforgeeks.org/wp-content/cdn-uploads/20200811210521/Collection-Framework-1.png
Collection Interface Hierarchy from https://media.geeksforgeeks.org/wp-content/cdn-uploads/20200811210521/Collection-Framework-1.png

— — — — — — —

Press enter or click to view image in full size
Map Interface Hierarchy from https://media.geeksforgeeks.org/wp-content/cdn-uploads/20200811210611/Collection-Framework-2.png

--

--