In Part 1, we covered ArrayList
, LinkedList
, Queue
, Stack
, and Deque
. Now, in Part 2, we’ll explore HashSet, LinkedHashSet, TreeSet, HashMap, LinkedHashMap, and TreeMap, ensuring we cover all their methods with explanations and examples.
1. HashSet in Java
A HashSet is an unordered collection that does not allow duplicate elements. It is implemented using a HashMap internally.
Declaration and Initialization
import java.util.HashSet;
import java.util.Set;
Set<Integer> set = new HashSet<>();
Methods of HashSet
Method | Description |
---|---|
add(E e) |
Adds an element to the set. |
remove(Object o) |
Removes an element if present. |
contains(Object o) |
Checks if an element exists. |
size() |
Returns the number of elements. |
clear() |
Removes all elements. |
isEmpty() |
Checks if the set is empty. |
toArray() |
Converts the set to an array. |
retainAll(Collection> c) |
Retains only elements also present in another collection. |
removeAll(Collection> c) |
Removes all elements present in another collection. |
Example:
import java.util.HashSet;
public class HashSetExample {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Cherry");
set.add("Banana"); // Duplicate, ignored
System.out.println(set.contains("Apple")); // true
set.remove("Banana");
System.out.println(set); // [Apple, Cherry] (unordered)
}
}
2. LinkedHashSet in Java
A LinkedHashSet maintains the insertion order while ensuring uniqueness.
Declaration and Initialization
import java.util.LinkedHashSet;
import java.util.Set;
Set<String> linkedHashSet = new LinkedHashSet<>();
Methods of LinkedHashSet
(Same as HashSet
, but maintains insertion order)
Example:
import java.util.LinkedHashSet;
public class LinkedHashSetExample {
public static void main(String[] args) {
LinkedHashSet<Integer> numbers = new LinkedHashSet<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(20); // Duplicate ignored
System.out.println(numbers); // [10, 20, 30] (maintains order)
}
}
3. TreeSet in Java
A TreeSet stores elements in sorted order using a Red-Black Tree.
Declaration and Initialization
import java.util.TreeSet;
import java.util.Set;
Set<Integer> treeSet = new TreeSet<>();
Methods of TreeSet
Method | Description |
---|---|
add(E e) |
Adds an element while maintaining sorting. |
remove(Object o) |
Removes an element if present. |
contains(Object o) |
Checks if an element exists. |
size() |
Returns the number of elements. |
clear() |
Removes all elements. |
isEmpty() |
Checks if the set is empty. |
first() |
Returns the first (smallest) element. |
last() |
Returns the last (largest) element. |
higher(E e) |
Returns the smallest element greater than e . |
lower(E e) |
Returns the largest element smaller than e . |
subSet(E from, E to) |
Returns a subset in range [from, to) . |
Example:
import java.util.TreeSet;
public class TreeSetExample {
public static void main(String[] args) {
TreeSet<Integer> sortedSet = new TreeSet<>();
sortedSet.add(50);
sortedSet.add(10);
sortedSet.add(30);
System.out.println(sortedSet); // [10, 30, 50] (sorted)
System.out.println(sortedSet.first()); // 10
System.out.println(sortedSet.last()); // 50
}
}
4. HashMap in Java
A HashMap stores key-value pairs in an unordered fashion.
Declaration and Initialization
import java.util.HashMap;
import java.util.Map;
Map<String, Integer> map = new HashMap<>();
Methods of HashMap
Method | Description |
---|---|
put(K key, V value) |
Inserts a key-value pair. |
get(Object key) |
Retrieves value for the given key. |
remove(Object key) |
Removes a key-value pair. |
containsKey(Object key) |
Checks if a key exists. |
containsValue(Object value) |
Checks if a value exists. |
size() |
Returns the number of elements. |
isEmpty() |
Checks if the map is empty. |
keySet() |
Returns a set of keys. |
values() |
Returns a collection of values. |
entrySet() |
Returns a set of key-value pairs. |
Example:
import java.util.HashMap;
public class HashMapExample {
public static void main(String[] args) {
HashMap<String, Integer> studentMarks = new HashMap<>();
studentMarks.put("Alice", 85);
studentMarks.put("Bob", 90);
studentMarks.put("Charlie", 80);
System.out.println(studentMarks.get("Alice")); // 85
studentMarks.remove("Charlie");
System.out.println(studentMarks); // {Alice=85, Bob=90}
}
}
5. LinkedHashMap in Java
A LinkedHashMap maintains insertion order while storing key-value pairs.
Declaration and Initialization
import java.util.LinkedHashMap;
import java.util.Map;
Map<String, Integer> linkedMap = new LinkedHashMap<>();
Methods of LinkedHashMap
(Same as HashMap
, but maintains insertion order)
Example:
import java.util.LinkedHashMap;
public class LinkedHashMapExample {
public static void main(String[] args) {
LinkedHashMap<String, Integer> linkedMap = new LinkedHashMap<>();
linkedMap.put("A", 100);
linkedMap.put("B", 200);
linkedMap.put("C", 300);
System.out.println(linkedMap); // {A=100, B=200, C=300} (order maintained)
}
}
6. TreeMap in Java
A TreeMap stores key-value pairs in sorted order based on keys.
Declaration and Initialization
import java.util.TreeMap;
import java.util.Map;
Map<Integer, String> treeMap = new TreeMap<>();
Methods of TreeMap
Method | Description |
---|---|
put(K key, V value) |
Inserts a key-value pair while maintaining sorting. |
get(Object key) |
Retrieves value for the given key. |
remove(Object key) |
Removes a key-value pair. |
containsKey(Object key) |
Checks if a key exists. |
firstKey() |
Returns the first (smallest) key. |
lastKey() |
Returns the last (largest) key. |
higherKey(K key) |
Returns the smallest key greater than key . |
lowerKey(K key) |
Returns the largest key smaller than key . |
Example:
import java.util.TreeMap;
public class TreeMapExample {
public static void main(String[] args) {
TreeMap<Integer, String> sortedMap = new TreeMap<>();
sortedMap.put(3, "Three");
sortedMap.put(1, "One");
sortedMap.put(2, "Two");
System.out.println(sortedMap); // {1=One, 2=Two, 3=Three} (sorted by keys)
}
}
🚀 With this, we’ve covered all major Java Collections!