Aug 16, 2024•1 min read
Working with Arrays & Collections Summary
A quick-reference summary for arrays, collections, generics, sorting/searching, null behavior, and comparators in Java.
title: "Working with Arrays & Collections Summary" date: "2024-08-16" excerpt: "A quick-reference summary for arrays, collections, generics, sorting/searching, null behavior, and comparators in Java."
tags: "java,collections,arrays" source: "https://shrikant-havale.in/2024/08/16/working-with-arrays-collections-summary/"
This is a focused summary of practical Java collection and array behavior.
Deque Notes
ArrayDequeis aDequeimplementation.- Useful operations:
- Add to tail:
offer,offerLast - Read head:
peek,peekFirst - Push front:
push,offerFirst,addFirst - Remove head:
poll,pop,removeFirst - Remove tail:
removeLast
- Add to tail:
Arrays
- With
{...}initializer, size is inferred. - Without initializer, size is required.
Arrays.sort(...)andArrays.binarySearch(...)are standard methods.binarySearchexpects correctly sorted input.Arrays.mismatch(a, b)returns first differing index, or-1if equal.
Generics and Wildcards
?wildcard is valid in declarations but not when instantiating concrete types on RHS.- Always prefer explicit type parameters on declarations.
- Diamond operator
<>helps avoid duplicate type declarations.
Collections
Mapis part of the Collections Framework but does not implementCollection.Collections.binarySearch(...)requires matching sort order and comparator.
Null Handling
TreeSetrejectsnulldue to comparison requirements.HashSetallows onenull.ArrayListandLinkedListallow multiplenullvalues.
Comparable vs Comparator
Comparable<T>=> implementcompareTo(T).Comparator<T>=> implementcompare(T, T).- Comparator chains and reversals (
reversed()) are useful for custom sort logic.
Immutable Factories
List.of(...),Set.of(...)=> immutable collections from values.List.copyOf(...),Set.copyOf(...)=> immutable copies.
Source
Original post: https://shrikant-havale.in/2024/08/16/working-with-arrays-collections-summary/