The Java Collections Framework (JCF) provides a set of predefined classes and interfaces to store, manipulate, and retrieve groups of objects efficiently. Instead of handling arrays manually, collections offer dynamic sizing, built-in algorithms, and high performance. Collections are widely used in enterprise applications, interviews, and real-world projects.
At the top of the hierarchy is the Collection interface, which is part of java.util package.
Iterable
↓
Collection
├── List
├── Set
└── Queue
Map (separate hierarchy)
Collection is the root interface for List, Set, and QueueMap does not extend CollectionA List is an ordered collection that allows:
Characteristics
Example
import java.util.*;
public class Demo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
list.add("Java"); // duplicates allowed
System.out.println(list);
}
}
Characteristics
Example
LinkedList<Integer> numbers = new LinkedList<>();
numbers.add(10);
numbers.add(20);
numbers.addFirst(5);
System.out.println(numbers);
Characteristics
Example
Vector<String> v = new Vector<>();
v.add("A");
v.add("B");
System.out.println(v);
A Set stores unique elements only and does not allow duplicates.
Characteristics
Example
HashSet<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Apple");
System.out.println(set);
Characteristics
Example
LinkedHashSet<Integer> lhs = new LinkedHashSet<>();
lhs.add(10);
lhs.add(5);
lhs.add(20);
System.out.println(lhs);
Characteristics
Example
TreeSet<Integer> ts = new TreeSet<>();
ts.add(30);
ts.add(10);
ts.add(20);
System.out.println(ts);
A Map stores data in key-value pairs.
Characteristics
Example
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "Python");
System.out.println(map);
Characteristics
Example
LinkedHashMap<Integer, String> lhm = new LinkedHashMap<>();
lhm.put(1, "A");
lhm.put(2, "B");
System.out.println(lhm);
Characteristics
Example
TreeMap<Integer, String> tm = new TreeMap<>();
tm.put(3, "C");
tm.put(1, "A");
tm.put(2, "B");
System.out.println(tm);
Description
Example
Queue<Integer> q = new LinkedList<>();
q.add(10);
q.add(20);
q.poll();
System.out.println(q);
Description
Example
Deque<Integer> dq = new ArrayDeque<>();
dq.addFirst(10);
dq.addLast(20);
System.out.println(dq);
Used to traverse collections safely.
Example
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
Iterator<String> itr = list.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
Simpler way to iterate.
Example
for (String lang : list) {
System.out.println(lang);
}
Used for natural sorting order.
Example
class Student implements Comparable<Student> {
int age;
Student(int age) {
this.age = age;
}
public int compareTo(Student s) {
return this.age - s.age;
}
}
Used for custom sorting logic.
Example
Comparator<Student> ageComparator = new Comparator<Student>() {
public int compare(Student s1, Student s2) {
return s2.age - s1.age;
}
};
| Comparable | Comparator |
|---|---|
| Natural sorting | Custom sorting |
| compareTo() | compare() |
| One sorting logic | Multiple sorting logics |
java
import java.util.*;
class Person implements Comparable<Person> {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(Person other) {
return this.age - other.age;
}
@Override
public String toString() {
return name + " (" + age + ")";
}
}
public class CollectionsDemo {
public static void main(String[] args) {
// ArrayList
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
System.out.println("Fruits: " + fruits);
// HashSet (no duplicates)
Set<Integer> numbers = new HashSet<>();
numbers.add(10);
numbers.add(20);
numbers.add(10); // Duplicate, won't be added
System.out.println("Numbers: " + numbers);
// HashMap
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 95);
scores.put("Bob", 87);
scores.put("Charlie", 92);
for (Map.Entry<String, Integer> entry : scores.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
// TreeSet (sorted)
Set<Person> people = new TreeSet<>();
people.add(new Person("John", 30));
people.add(new Person("Alice", 25));
people.add(new Person("Bob", 35));
System.out.println("People sorted by age: " + people);
// Queue
Queue<String> queue = new LinkedList<>();
queue.offer("First");
queue.offer("Second");
queue.offer("Third");
System.out.println("Queue poll: " + queue.poll());
// Sorting with Comparator
List<Person> personList = new ArrayList<>();
personList.add(new Person("John", 30));
personList.add(new Person("Alice", 25));
Collections.sort(personList, (p1, p2) -> p1.name.compareTo(p2.name));
System.out.println("Sorted by name: " + personList);
}
}