The main advantage of ArrayList is that, unlike normal arrays, we don’t need to mention the size when creating one. It automatically adjusts its capacity as elements are added or removed. It may be slower than standard arrays, but it is helpful when the size is not known in advance. Note that creating a large fixed-sized array would cause a waste of space.
Since ArrayList is part of the collections framework, it has better interoperability
with other collections. For example, conversion to a HashSet is straightforward. With
generics, ArrayList<T> ensures type safety at compile-time.
CopyOnWriteArrayList: This class implements the list interface. It is an enhanced version of ArrayList in which all modifications (add, set, remove, etc.) are implemented by making a fresh copy of the list.
Key Points:
The Vector class implements a growable array of objects.
Although it falls under legacy classes, it is fully compatible with the collections
framework and is found in the java.util package.
Highlights:
Additional Info: The iterators returned by Vector are fail-fast (throwing ConcurrentModificationException upon concurrent modifications).
The Stack class, provided by the Java Collection Framework, models a stack data structure based on LIFO (last-in-first-out). It extends Vector and provides additional operations specific to stack behavior.
Operations include: push, pop, peek, empty, and search.
The Set interface, present in the java.util
package, is an unordered collection of objects that does not allow duplicate values. It
implements the mathematical set concept.
Key Characteristics:
EnumSet is a specialized set implementation for enum
types. Part of the java.util package, it provides a highly optimized set
for storing enum constants.
Key Details:
HashSet implements the Set interface and is used to store unique elements without maintaining any specific order.
Key Points:
LinkedHashSet combines the uniqueness of a HashSet with the predictable iteration order of a LinkedList by maintaining a doubly-linked list of its elements.
Highlights:
The SortedSet interface (in java.util)
extends Set and ensures that its elements are stored in a sorted (ascending) order.
Features:
NavigableSet is a subtype of SortedSet that provides
navigation methods such as first(), last(),
headSet(), tailSet(), etc.
It allows both ascending and descending order iteration. The most common implementation
is TreeSet.
TreeSet implements the SortedSet interface using a self-balancing binary search tree (red–black tree). It maintains elements in sorted order based on natural ordering or via a provided Comparator.
Details:
higher(),
lower(), ceiling(), and floor()).
Note on Synchronized TreeSet: To synchronize, wrap the
set with Collections.synchronizedSortedSet() at creation time.
The PriorityQueue class processes elements according to their natural ordering or via a provided Comparator rather than strictly FIFO.
Highlights:
add and poll are O(log n).PriorityBlockingQueue for concurrent environments.
public class Main {
public static void main(String[] args) {
// Priority Queue (Min-Heap)
PriorityQueue p = new PriorityQueue<>();
p.add(3);
p.add(10);
p.add(7);
p.add(2);
System.out.println("Head of Queue : " + p.peek());
}
}
Output: Head of Queue : 2
TreeMap is part of the Java Collections Framework and implements both the Map and NavigableMap interfaces. It stores key-value pairs in sorted order using a red–black tree.
Key Details:
get, put, and remove run in
O(log n) time.
Internal Structure: Each node has key, value, color, and references to left, right, and parent.
The HashMap class provides the basic implementation of the Map interface using key-value pairs. It uses hashing for efficient retrieval.
Characteristics:
Internal Structure: Each node contains an integer hash, key, value, and a pointer to the next node.
The Hashtable class implements a hash table that maps keys to values. It is similar to HashMap but is synchronized.
Key Points:
Advantages: Thread-safe and simple to use.
Disadvantages: Considered obsolete, offers limited functionality, and can have poorer performance compared to newer Map implementations.
SortedMap is an interface in the Java Collections Framework that represents a map which maintains its keys in a sorted order.
Details:
With generics, SortedMap becomes type-safe.
ConcurrentHashMap is a thread-safe implementation of the Map interface. It allows concurrent read and write operations without locking the entire map.
Features:
putIfAbsent(),
replace(), and remove().
EnumMap is a specialized implementation of the Map interface for use with enumeration types. It is highly efficient and compact.
Key Features:
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public class Geeks {
public static void main(String[] args) {
EnumMap dayMap = new EnumMap<>(Day.class);
dayMap.put(Day.MONDAY, "Start of the week");
dayMap.put(Day.FRIDAY, "End of the week");
dayMap.put(Day.SUNDAY, "Weekend");
for (Day day : dayMap.keySet()) {
System.out.println(day + ": " + dayMap.get(day));
}
}
}
The Map.Entry interface provides methods to access and
manipulate key-value pairs within a Map. It is often used in conjunction with the entrySet()
method.
The Map interface, part of java.util,
represents a mapping between keys and values. It is not a subtype of Collection and
behaves differently than other collection types.
Key Characteristics:
LinkedHashMap implements the Map interface and maintains the insertion order of its key-value pairs. It combines the benefits of HashMap with predictable iteration.
Highlights:
NavigableMap is an extension of the SortedMap interface that provides additional navigation methods for dealing with keys. It supports finding entries for keys that are less than, greater than, or equal to a given key.
Methods include: lowerKey(), higherEntry(),
floorEntry(), ceilingEntry(), and submap methods (subMap(),
headMap(), tailMap()).
The ArrayDeque class is a resizable array implementation of the Deque interface. It supports element insertion and removal at both ends and can be used as both a stack (LIFO) and a queue (FIFO).
Key Features:
public class Geeks {
public static void main(String[] args) {
Deque d = new ArrayDeque<>();
d.addFirst(1);
d.addLast(2);
int f = d.removeFirst();
int l = d.removeLast();
System.out.println("First: " + f + ", Last: " + l);
}
}
The Queue interface, present in java.util,
stores and processes elements in a FIFO (First-In-First-Out) order. It limits insertions
to the end of the list and deletions from the start.
Key Points:
public class QueueCreation {
public static void main(String args[]) {
// Create a Queue of Integers using LinkedList
Queue q = new LinkedList<>();
System.out.println("Queue elements: " + q);
}
}
My simple library
..of useful code