Collection Framework of Java
The Java Collection Framework provides a set of interfaces and classes that allow you to work with groups of objects in a standardized way. The framework is divided into several interfaces and classes, including `List`, `Set`, `Queue`, `Map`, and their various implementations.
Here’s an overview of the main interfaces and classes in the Java Collection Framework, along with examples for each:
### 1. **List Interface**
- **Characteristics**: Ordered collection (sequence) that allows duplicates. Elements can be accessed by their index.
#### Implementations:
- **ArrayList**: Resizable array implementation of the `List` interface.
- **LinkedList**: Doubly-linked list implementation of the `List` and `Deque` interfaces.
- **Vector**: Synchronized, resizable array implementation.
- **Stack**: A subclass of `Vector` that represents a last-in, first-out (LIFO) stack.
#### Example:
```java
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Vector;
import java.util.Stack;
public class ListExample {
public static void main(String[] args) {
// ArrayList example
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
System.out.println("ArrayList: " + arrayList);
// LinkedList example
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("X");
linkedList.add("Y");
linkedList.add("Z");
System.out.println("LinkedList: " + linkedList);
// Vector example
Vector<Integer> vector = new Vector<>();
vector.add(1);
vector.add(2);
vector.add(3);
System.out.println("Vector: " + vector);
// Stack example
Stack<String> stack = new Stack<>();
stack.push("First");
stack.push("Second");
stack.push("Third");
System.out.println("Stack: " + stack);
}
}
```
### 2. **Set Interface**
- **Characteristics**: Unordered collection that does not allow duplicates.
#### Implementations:
- **HashSet**: Implements the `Set` interface, backed by a hash table.
- **LinkedHashSet**: Ordered version of `HashSet` that maintains a doubly-linked list of entries.
- **TreeSet**: Implements the `Set` interface, backed by a `TreeMap`. Maintains sorted order.
#### Example:
```java
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;
public class SetExample {
public static void main(String[] args) {
// HashSet example
HashSet<String> hashSet = new HashSet<>();
hashSet.add("Banana");
hashSet.add("Apple");
hashSet.add("Orange");
hashSet.add("Banana"); // Duplicate, won't be added
System.out.println("HashSet: " + hashSet);
// LinkedHashSet example
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("One");
linkedHashSet.add("Two");
linkedHashSet.add("Three");
System.out.println("LinkedHashSet: " + linkedHashSet);
// TreeSet example
TreeSet<String> treeSet = new TreeSet<>();
treeSet.add("Cat");
treeSet.add("Dog");
treeSet.add("Elephant");
System.out.println("TreeSet: " + treeSet);
}
}
```
### 3. **Queue Interface**
- **Characteristics**: Used to hold multiple elements prior to processing. Typically follows FIFO (First-In, First-Out) order.
#### Implementations:
- **LinkedList**: Implements the `Queue` interface. Also implements the `Deque` interface.
- **PriorityQueue**: Implements the `Queue` interface and orders its elements according to their natural order or by a comparator.
#### Example:
```java
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args) {
// LinkedList as Queue
Queue<String> linkedListQueue = new LinkedList<>();
linkedListQueue.offer("A");
linkedListQueue.offer("B");
linkedListQueue.offer("C");
System.out.println("LinkedList Queue: " + linkedListQueue);
System.out.println("Poll: " + linkedListQueue.poll()); // Removes and returns the head
// PriorityQueue example
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
priorityQueue.offer(3);
priorityQueue.offer(1);
priorityQueue.offer(2);
System.out.println("PriorityQueue: " + priorityQueue);
System.out.println("Poll: " + priorityQueue.poll()); // Removes and returns the smallest element
}
}
```
### 4. **Map Interface**
- **Characteristics**: Maps keys to values. Each key can map to at most one value. Does not allow duplicate keys.
#### Implementations:
- **HashMap**: Implements the `Map` interface, backed by a
hash table. Allows null values and one null key. It does not guarantee any specific order of its elements.
- **LinkedHashMap**: Extends `HashMap` and maintains a linked list of the entries in the map, preserving the insertion order.
- **TreeMap**: Implements the `Map` interface and stores its entries in a sorted order according to the natural ordering of its keys or by a comparator.
- **Hashtable**: Similar to `HashMap`, but synchronized and does not allow null keys or values.
#### Example:
```java
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.TreeMap;
import java.util.Hashtable;
public class MapExample {
public static void main(String[] args) {
// HashMap example
HashMap<String, Integer> hashMap = new HashMap<>();
hashMap.put("One", 1);
hashMap.put("Two", 2);
hashMap.put("Three", 3);
System.out.println("HashMap: " + hashMap);
// LinkedHashMap example
LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("Apple", 10);
linkedHashMap.put("Banana", 20);
linkedHashMap.put("Orange", 30);
System.out.println("LinkedHashMap: " + linkedHashMap);
// TreeMap example
TreeMap<String, Integer> treeMap = new TreeMap<>();
treeMap.put("Zebra", 26);
treeMap.put("Lion", 15);
treeMap.put("Elephant", 30);
System.out.println("TreeMap: " + treeMap);
// Hashtable example
Hashtable<String, Integer> hashtable = new Hashtable<>();
hashtable.put("India", 91);
hashtable.put("USA", 1);
hashtable.put("UK", 44);
System.out.println("Hashtable: " + hashtable);
}
}
```
### 5. **Deque Interface**
- **Characteristics**: A double-ended queue that allows elements to be added or removed from both ends.
#### Implementations:
- **ArrayDeque**: Resizable-array implementation of the `Deque` interface. More efficient than `LinkedList` when used as a stack or queue.
#### Example:
```java
import java.util.ArrayDeque;
import java.util.Deque;
public class DequeExample {
public static void main(String[] args) {
Deque<String> arrayDeque = new ArrayDeque<>();
arrayDeque.addFirst("First");
arrayDeque.addLast("Last");
arrayDeque.addFirst("New First");
System.out.println("ArrayDeque: " + arrayDeque);
System.out.println("Remove First: " + arrayDeque.removeFirst());
System.out.println("Remove Last: " + arrayDeque.removeLast());
System.out.println("ArrayDeque after removals: " + arrayDeque);
}
}
```
### Summary:
The Java Collection Framework includes a variety of classes and interfaces to handle collections of objects in a standardized way. Here's a quick summary of the main interfaces and their typical implementations:
- **List**: Ordered collection that allows duplicates (`ArrayList`, `LinkedList`, `Vector`, `Stack`).
- **Set**: Unordered collection that does not allow duplicates (`HashSet`, `LinkedHashSet`, `TreeSet`).
- **Queue**: Collection that processes elements in FIFO order (`LinkedList`, `PriorityQueue`).
- **Map**: Collection that maps keys to values (`HashMap`, `LinkedHashMap`, `TreeMap`, `Hashtable`).
- **Deque**: Double-ended queue that allows element addition/removal at both ends (`ArrayDeque`).
These examples give you a basic understanding of how to use the Java Collection Framework in different scenarios.
Comments
Post a Comment