Java Collections Framework Part 2

Summary - Java Collections Framework - Part 2

Queue Interface

The Queue interface is used to hold multiple elements prior to processing, typically in a FIFO (First-In-First-Out) manner.
Common Implementations:
LinkedList: Implements Queue and provides methods for queue operations.
PriorityQueue: An unbounded priority queue based on a priority heap.
Key Methods:
add(E e): Inserts the specified element into the queue. Throws an exception if it fails.
offer(E e): Inserts the specified element into the queue. Returns false if it fails.
remove(): Retrieves and removes the head of the queue. Throws an exception if the queue is empty.
poll(): Retrieves and removes the head of the queue. Returns null if the queue is empty.
element(): Retrieves, but does not remove, the head of the queue. Throws an exception if the queue is empty.
peek(): Retrieves, but does not remove, the head of the queue. Returns null if the queue is empty.

Set Interface

The Set interface represents a collection that does not allow duplicate elements.
Common Implementations:
HashSet: Uses a hash table for storage. Unordered.
LinkedHashSet: HashSet with predictable iteration order.
TreeSet: Implements SortedSet, backed by a TreeMap. Elements are sorted.
Key Methods:
add(E e): Adds the specified element to the set if not already present.
remove(Object o): Removes the specified element from the set if present.
contains(Object o): Returns true if the set contains the specified element.
size(): Returns the number of elements in the set.
clear(): Removes all elements from the set.

Map Interface

The Map interface maps unique keys to values.
Common Implementations:
HashMap: Uses a hash table for storage. Allows null values and one null key.
LinkedHashMap: HashMap with predictable iteration order.
TreeMap: Implements SortedMap, backed by a TreeMap. Elements are sorted by keys.
Key Methods:
put(K key, V value): Associates the specified value with the specified key in this map.
get(Object key): Returns the value to which the specified key is mapped.
remove(Object key): Removes the mapping for a key from this map if present.
containsKey(Object key): Returns true if this map contains a mapping for the specified key.
containsValue(Object value): Returns true if this map maps one or more keys to the specified value.
keySet(): Returns a Set view of the keys contained in this map.
values(): Returns a Collection view of the values contained in this map.
entrySet(): Returns a Set view of the mappings contained in this map.

Using Custom Classes in Sets & Maps

When using custom classes as elements in sets or as keys in maps, it is crucial to override the equals() and hashCode() methods to ensure correct behavior.
Example of Using a Custom Class in a Set:
Custom Class: Create a class with overridden equals() and hashCode() methods to ensure correct behavior in sets.
Example of Using a Custom Class as a Key in a Map:
Custom Class: Create a class with overridden equals() and hashCode() methods to ensure correct behavior as keys in maps.
Understanding Java Collections provides developers with more flexibility over data structures. For more detailed information, refer to the Notion link:
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.