Collections in Java
Collections in Java are used to store and manipulate groups of objects. They provide a way to manage data efficiently and perform operations like searching, sorting, and modifying elements. In this lesson, we will explore the most commonly used collection types.
List
A List is an ordered collection that allows duplicate elements. It provides methods to access, insert, and remove elements by their position (index).
Vector
Vector is a legacy collection class that implements a dynamic array. It is synchronized, meaning it is thread-safe, but this makes it slower compared to other list implementations.
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
Vector<String> vector = new Vector<>();
vector.add("Apple");
vector.add("Banana");
vector.add("Cherry");
System.out.println("Vector: " + vector);
}
}
Vector
only when thread safety is required.
Otherwise, prefer ArrayList
.
ArrayList
ArrayList is a resizable array implementation of
the List interface. It is not synchronized, making it faster than
Vector
. It allows random access to elements because it
uses an array internally.
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Dog");
arrayList.add("Cat");
arrayList.add("Elephant");
System.out.println("ArrayList: " + arrayList);
}
}
LinkedList
LinkedList is implemented as a doubly-linked list.
It is efficient for adding and removing elements from both ends of
the list but slower for random access compared to
ArrayList
.
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("Red");
linkedList.add("Green");
linkedList.add("Blue");
System.out.println("LinkedList: " + linkedList);
}
}
Set
A Set is a collection that does not allow duplicate elements. It models the mathematical set abstraction.
HashSet
HashSet is an unordered collection that does not maintain any specific order of elements. It uses a hash table for storage, providing constant-time performance for basic operations.
import java.util.HashSet;
public class HashSetExample {
public static void main(String[] args) {
HashSet<String> hashSet = new HashSet<>();
hashSet.add("Java");
hashSet.add("Python");
hashSet.add("C++");
System.out.println("HashSet: " + hashSet);
}
}
LinkedHashSet
LinkedHashSet maintains the insertion order of elements while still providing the benefits of a hash table.
import java.util.LinkedHashSet;
public class LinkedHashSetExample {
public static void main(String[] args) {
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("One");
linkedHashSet.add("Two");
linkedHashSet.add("Three");
System.out.println("LinkedHashSet: " + linkedHashSet);
}
}
TreeSet
TreeSet is a sorted collection that orders elements according to their natural ordering or a specified comparator.
import java.util.TreeSet;
public class TreeSetExample {
public static void main(String[] args) {
TreeSet<Integer> treeSet = new TreeSet<>();
treeSet.add(10);
treeSet.add(5);
treeSet.add(20);
System.out.println("TreeSet: " + treeSet);
}
}
Queue
A Queue is a collection designed for holding elements prior to processing. It follows the First-In-First-Out (FIFO) principle.
PriorityQueue
PriorityQueue orders elements according to their natural ordering or a custom comparator. It does not follow strict FIFO order.
import java.util.PriorityQueue;
public class PriorityQueueExample {
public static void main(String[] args) {
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
priorityQueue.add(10);
priorityQueue.add(5);
priorityQueue.add(20);
System.out.println("PriorityQueue: " + priorityQueue);
}
}
Deque
A Deque (Double-Ended Queue) is a linear collection that supports element insertion and removal at both ends.
ArrayDeque
ArrayDeque is a resizable-array implementation of
the Deque interface. It is faster than LinkedList
for
most operations.
import java.util.ArrayDeque;
public class ArrayDequeExample {
public static void main(String[] args) {
ArrayDeque<String> deque = new ArrayDeque<>();
deque.addFirst("Front");
deque.addLast("Back");
System.out.println("ArrayDeque: " + deque);
}
}
Map
A Map is an object that maps keys to values. It cannot contain duplicate keys, and each key can map to at most one value.
HashMap
HashMap stores key-value pairs and does not maintain any order. It allows one null key and multiple null values.
import java.util.HashMap;
public class HashMapExample {
public static void main(String[] args) {
HashMap<String, Integer> hashMap = new HashMap<>();
hashMap.put("John", 25);
hashMap.put("Jane", 30);
hashMap.put("Doe", 22);
System.out.println("HashMap: " + hashMap);
}
}
TreeMap
TreeMap is a sorted map implementation that orders its entries based on the natural ordering of keys or a custom comparator.
import java.util.TreeMap;
public class TreeMapExample {
public static void main(String[] args) {
TreeMap<String, Integer> treeMap = new TreeMap<>();
treeMap.put("John", 25);
treeMap.put("Jane", 30);
treeMap.put("Doe", 22);
System.out.println("TreeMap: " + treeMap);
}
}
Hashtable
Hashtable is a synchronized map that does not allow
null keys or values. It is similar to HashMap
but is
thread-safe.
import java.util.Hashtable;
public class HashtableExample {
public static void main(String[] args) {
Hashtable<String, Integer> hashtable = new Hashtable<>();
hashtable.put("John", 25);
hashtable.put("Jane", 30);
hashtable.put("Doe", 22);
System.out.println("Hashtable: " + hashtable);
}
}