Sitemap
Raghunath

Raghunath

Java Collections

--

Java Collections Framework is the foundation for storing, manipulating, and accessing data in Java. This article provides a complete and structured overview of all major collection types, interfaces, and operations with real-world Java examples, expected outputs, and detailed explanations.

What are Java Collections?

Java Collections is a framework that provides a set of interfaces and classes to store and manipulate groups of data efficiently. It includes Lists, Sets, Queues, Maps, and more.

Core Interfaces:

  • List — Ordered collection, allows duplicates (e.g., ArrayList, LinkedList)
  • Set — Unordered, no duplicates (e.g., HashSet, LinkedHashSet, TreeSet)
  • Queue — FIFO structure (e.g., PriorityQueue, Deque)
  • Map — Key-value pairs (e.g., HashMap, TreeMap, LinkedHashMap)

List Interface — ArrayList

ArrayList is a resizable array implementation of the List interface. It allows random access and maintains insertion order.

import java.util.*;

public class ListExample {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Alice");

System.out.println("List: " + names);
System.out.println("Size: " + names.size());
System.out.println("Contains 'Bob'? " + names.contains("Bob"));
names.remove("Alice");
System.out.println("After removing 'Alice': " + names);
System.out.println("Is Empty? " + names.isEmpty());
System.out.println("To Array: " + Arrays.toString(names.toArray()));
}
}
Output:
List: [Alice, Bob, Alice]
Size: 3
Contains 'Bob'? true
After removing 'Alice': [Bob, Alice]
Is Empty? false
To Array: [Bob, Alice]

Set Interface — HashSet and LinkedHashSet

  • HashSet: Does not maintain order and doesn’t allow duplicates.
  • LinkedHashSet: Maintains insertion order and also avoids duplicates.
import java.util.*;

public class SetExample {
public static void main(String[] args) {
Set<Integer> numbers = new HashSet<>();
numbers.add(10);
numbers.add(20);
numbers.add(10);
System.out.println("HashSet: " + numbers);

Set<Integer> linked = new LinkedHashSet<>();
linked.add(5);
linked.add(3);
linked.add(5);
System.out.println("LinkedHashSet: " + linked);
}
}
Output:
HashSet: [20, 10] or [10, 20]
LinkedHashSet: [5, 3]

TreeSet

TreeSet stores elements in sorted (natural) order and does not allow duplicates.

import java.util.*;

public class TreeSetExample {
public static void main(String[] args) {
Set<String> cities = new TreeSet<>();
cities.add("Mumbai");
cities.add("Delhi");
cities.add("Bangalore");

System.out.println("Sorted TreeSet: " + cities);
}
}
Output:
Sorted TreeSet: [Bangalore, Delhi, Mumbai]

Queue — PriorityQueue

PriorityQueueretrieves elements based on priority (natural ordering by default).

import java.util.*;

public class QueueExample {
public static void main(String[] args) {
Queue<Integer> pq = new PriorityQueue<>();
pq.add(30);
pq.add(10);
pq.add(20);

System.out.println("PriorityQueue: " + pq);
System.out.println("Poll: " + pq.poll());
System.out.println("After poll: " + pq);
}
}
PriorityQueue: [10, 30, 20] (order may vary)
Poll: 10
After poll: [20, 30]

Deque — ArrayDeque

Deque is a double-ended queue supporting element insertion/removal at both ends.

import java.util.*;

public class DequeExample {
public static void main(String[] args) {
Deque<String> dq = new ArrayDeque<>();
dq.addFirst("Front");
dq.addLast("Back");
System.out.println("Deque: " + dq);
dq.removeFirst();
System.out.println("After removeFirst: " + dq);
}
}
Output:
Deque: [Front, Back]
After removeFirst: [Back]

Stack

Stack is a LIFO (last-in-first-out) collection extending Vector.

public class StackExample {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(100);
stack.push(200);
System.out.println("Stack: " + stack);
stack.pop();
System.out.println("After pop: " + stack);
}
}
Output:
Stack: [100, 200]
After pop: [100]

Map — HashMap

HashMap stores key-value pairs with no guaranteed order.

import java.util.*;

public class HashMapExample {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "One");
map.put(2, "Two");
System.out.println("Map: " + map);
map.remove(1);
System.out.println("After remove: " + map);
System.out.println("Contains key 2? " + map.containsKey(2));
}
}
Output:
Map: {1=One, 2=Two}
After remove: {2=Two}
Contains key 2? true

Common Collection Functions

import java.util.*;

public class CollectionFunctions {
public static void main(String[] args) {
List<String> list = new ArrayList<>(List.of("A", "B", "C"));
System.out.println("Original List: " + list);

System.out.println("Contains 'B'? " + list.contains("B"));
System.out.println("Size: " + list.size());

list.remove("B");
System.out.println("After remove: " + list);

list.add("D");
System.out.println("After add: " + list);

List<String> another = List.of("A", "D", "E");
list.retainAll(another);
System.out.println("After retainAll: " + list);

list.removeAll(List.of("D"));
System.out.println("After removeAll: " + list);

list.clear();
System.out.println("After clear: " + list);
System.out.println("Is Empty: " + list.isEmpty());

list = new ArrayList<>(List.of("X", "Y"));
System.out.println("To Array: " + Arrays.toString(list.toArray()));

System.out.println("Using Iterator:");
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}

System.out.println("Using stream:");
list.stream().forEach(System.out::println);

System.out.println("Using parallelStream:");
list.parallelStream().forEach(System.out::println);
}
}

Conclusion :

Get Raghunath’s stories in your inbox

Join Medium for free to get updates from this writer.

Java Collections Framework is a powerful and flexible toolset that helps you manage data efficiently. Whether you’re working with lists, maps, sets, or queues — understanding these core interfaces, classes, and their operations will make your code cleaner, more efficient, and production-ready.

--

--