Skip to content
Dev Dump

πŸš€ Java Collections Framework - Complete Guide

β€œThe Collections Framework provides a unified architecture for representing and manipulating collections, enabling you to focus on what you want to accomplish rather than how to accomplish it.” - Oracle Java Documentation

The Java Collections Framework is a unified architecture for representing and manipulating collections of objects. It provides a set of interfaces, implementations, and algorithms to handle groups of objects efficiently, eliminating the need to write custom data structures for most use cases.

benefits-collection

collection

e47c73756a31ba8b85648d4ad01c6475_MD5

2d47e4a6deba84be1dc09943b2fbc7a8_MD5

ImplementationAccessSearchInsertionDeletionOrderingThread-Safe
ArrayListO(1)O(n)O(1) amortizedO(n)Insertion❌
LinkedListO(n)O(n)O(1)O(1)Insertion❌
HashSetN/AO(1)O(1)O(1)No❌
TreeSetN/AO(log n)O(log n)O(log n)Sorted❌
HashMapO(1)O(1)O(1)O(1)No❌
TreeMapO(log n)O(log n)O(log n)O(log n)Sorted❌

implementation

methods


πŸ€” What type of data are you storing?
β”œβ”€β”€ πŸ”‘ Key-Value pairs?
β”‚ β”œβ”€β”€ Need sorting? β†’ TreeMap
β”‚ β”œβ”€β”€ Need insertion order? β†’ LinkedHashMap
β”‚ └── Best performance? β†’ HashMap
β”‚
β”œβ”€β”€ πŸ“‹ Unique elements only?
β”‚ β”œβ”€β”€ Need sorting? β†’ TreeSet
β”‚ β”œβ”€β”€ Need insertion order? β†’ LinkedHashSet
β”‚ └── Best performance? β†’ HashSet
β”‚
β”œβ”€β”€ πŸ“Š Ordered elements with duplicates?
β”‚ β”œβ”€β”€ Frequent random access? β†’ ArrayList
β”‚ β”œβ”€β”€ Frequent insertion/deletion? β†’ LinkedList
β”‚ └── Thread safety required? β†’ Vector or Collections.synchronizedList()
β”‚
└── 🎯 Processing queue?
β”œβ”€β”€ Priority-based? β†’ PriorityQueue
β”œβ”€β”€ FIFO operations? β†’ ArrayDeque
└── Both ends access? β†’ ArrayDeque
// Diamond operator (Java 7+)
List<String> list = new ArrayList<>();
Map<String, Integer> map = new HashMap<>();
// Factory methods (Java 9+)
List<String> immutableList = List.of("a", "b", "c");
Set<Integer> immutableSet = Set.of(1, 2, 3);
Map<String, Integer> immutableMap = Map.of("one", 1, "two", 2);
// Arrays.asList() for quick initialization
List<String> list = Arrays.asList("a", "b", "c");
// Collection copy constructors
List<String> copy = new ArrayList<>(original);
Set<String> uniqueItems = new HashSet<>(listWithDuplicates);

methods

// Enhanced for-loop
for (String item : collection) {
process(item);
}
// Iterator with removal
Iterator<String> it = collection.iterator();
while (it.hasNext()) {
String item = it.next();
if (shouldRemove(item)) {
it.remove(); // Safe removal during iteration
}
}
// Stream API
collection.stream()
.filter(predicate)
.map(mapper)
.collect(Collectors.toList());
// Sorting
Collections.sort(list);
Collections.sort(list, comparator);
// Searching
int index = Collections.binarySearch(sortedList, key);
// Min/Max
String min = Collections.min(collection);
String max = Collections.max(collection, comparator);
// Frequency and disjoint checks
int count = Collections.frequency(collection, item);
boolean noCommon = Collections.disjoint(collection1, collection2);