Java Collections
The Java Collections Framework provides a set of interfaces and classes to store and manipulate collections of objects. The main interfaces are List
, Set
, and Map
.
1. List
A List
is an ordered collection that allows duplicate elements. It maintains the order of elements based on their insertion sequence.
1.1 ArrayList
ArrayList
is a resizable array implementation of the List
interface.
Code Example:
import java.util.ArrayList;
import java.util.List;
public class ListExample {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
// Adding elements
names.add("Alice");
names.add("Bob");
names.add("Charlie");
// Accessing elements
System.out.println("First name: " + names.get(0));
// Removing elements
names.remove("Bob");
// Iterating through the list
for (String name : names) {
System.out.println(name);
}
}
}
1.2 LinkedList
LinkedList
is a doubly linked list implementation of the List
interface.
Code Example:
import java.util.LinkedList;
import java.util.List;
public class LinkedListExample {
public static void main(String[] args) {
List<String> names = new LinkedList<>();
// Adding elements
names.add("Alice");
names.add("Bob");
// Accessing elements
System.out.println("First name: " + names.get(0));
// Removing elements
names.remove("Bob");
// Iterating through the list
for (String name : names) {
System.out.println(name);
}
}
}
2. Set
A Set
is a collection that does not allow duplicate elements. It has no defined order for its elements.
2.1 HashSet
HashSet
is an implementation of the Set
interface that does not maintain any order of elements.
Code Example:
import java.util.HashSet;
import java.util.Set;
public class SetExample {
public static void main(String[] args) {
Set<String> names = new HashSet<>();
// Adding elements
names.add("Alice");
names.add("Bob");
names.add("Charlie");
names.add("Alice"); // Duplicate element
// Iterating through the set
for (String name : names) {
System.out.println(name);
}
}
}
2.2 TreeSet
TreeSet
is a Set
implementation that maintains elements in a sorted order.
Code Example:
import java.util.TreeSet;
import java.util.Set;
public class TreeSetExample {
public static void main(String[] args) {
Set<String> names = new TreeSet<>();
// Adding elements
names.add("Alice");
names.add("Bob");
names.add("Charlie");
// Iterating through the set
for (String name : names) {
System.out.println(name);
}
}
}
3. Map
A Map
is an object that maps keys to values. It does not allow duplicate keys but can have duplicate values.
3.1 HashMap
HashMap
is a Map
implementation that uses a hash table for storage. It does not maintain any order of its keys.
Code Example:
import java.util.HashMap;
import java.util.Map;
public class MapExample {
public static void main(String[] args) {
Map<String, Integer> ageMap = new HashMap<>();
// Adding key-value pairs
ageMap.put("Alice", 30);
ageMap.put("Bob", 25);
ageMap.put("Charlie", 35);
// Accessing a value
System.out.println("Alice's age: " + ageMap.get("Alice"));
// Iterating through the map
for (Map.Entry<String, Integer> entry : ageMap.entrySet()) {
System.out.println(entry.getKey() + " - " + entry.getValue());
}
}
}
3.2 TreeMap
TreeMap
is a Map
implementation that maintains its entries in sorted order based on the keys.
Code Example:
import java.util.TreeMap;
import java.util.Map;
public class TreeMapExample {
public static void main(String[] args) {
Map<String, Integer> ageMap = new TreeMap<>();
// Adding key-value pairs
ageMap.put("Alice", 30);
ageMap.put("Bob", 25);
ageMap.put("Charlie", 35);
// Iterating through the map
for (Map.Entry<String, Integer> entry : ageMap.entrySet()) {
System.out.println(entry.getKey() + " - " + entry.getValue());
}
}
}
Summary of Operations
- List: Allows duplicates and maintains order.
ArrayList
: Fast access, slow insertion/deletion.LinkedList
: Fast insertion/deletion, slow access.
- Set: No duplicates, unordered.
HashSet
: No order, fast operations.TreeSet
: Sorted order.
- Map: Maps keys to values.
HashMap
: No order, fast operations.TreeMap
: Sorted by keys.