Ugenz Academy/Tutorial

Collections

4 min read677 words

Reading progress

Keep learning

Collection Framework in Java

The Java Collection Framework is a set of interfaces and classes used to store, manage, and manipulate groups of objects.

It is mainly available in the java.util package.

Collections are useful when we need to store multiple values dynamically.

Main Collection Interfaces

The major interfaces are:

Collection ├── List ├── Set └── Queue Map

Map is part of the Collection Framework, but it does not extend the Collection interface.

1. List

A List is an ordered collection that allows duplicate elements.

Common implementations:

  • ArrayList
  • LinkedList
  • Vector
  • Stack

Example: ArrayList

import java.util.ArrayList; class Main { public static void main(String[] args) { ArrayList<String> students = new ArrayList<>(); students.add("Anuradha"); students.add("Ram"); students.add("Sita"); students.add("Anuradha"); System.out.println(students); } }

Output

[Anuradha, Ram, Sita, Anuradha]

2. Set

A Set is a collection that does not allow duplicate elements.

Common implementations:

  • HashSet
  • LinkedHashSet
  • TreeSet

Example

import java.util.HashSet; class Main { public static void main(String[] args) { HashSet<String> names = new HashSet<>(); names.add("Anuradha"); names.add("Ram"); names.add("Anuradha"); System.out.println(names); } }

The duplicate "Anuradha" is not stored.

3. Queue

A Queue is generally used to store elements in a processing order. A common queue follows FIFO (First In, First Out).

Common implementations:

  • PriorityQueue
  • ArrayDeque
  • LinkedList

Example

import java.util.LinkedList; import java.util.Queue; class Main { public static void main(String[] args) { Queue<String> queue = new LinkedList<>(); queue.add("A"); queue.add("B"); queue.add("C"); System.out.println(queue); System.out.println(queue.poll()); } }

Output

[A, B, C] A

4. Map

A Map stores data as key-value pairs. Keys are unique, while values can be duplicated.

Common implementations:

  • HashMap
  • LinkedHashMap
  • TreeMap
  • Hashtable

Example

import java.util.HashMap; class Main { public static void main(String[] args) { HashMap<Integer, String> students = new HashMap<>(); students.put(1, "Anuradha"); students.put(2, "Ram"); students.put(3, "Sita"); System.out.println(students); } }

Output

{1=Anuradha, 2=Ram, 3=Sita}

Common Collection Classes

ClassDescription
ArrayListResizable array; maintains insertion order
LinkedListList and deque implementation
HashSetStores unique elements without guaranteeing iteration order
TreeSetStores unique elements in sorted order
HashMapStores key-value pairs without guaranteeing iteration order
TreeMapStores key-value pairs sorted by keys
PriorityQueueProcesses elements according to priority

Common Collection Methods

MethodPurpose
add()Adds an element
remove()Removes an element
contains()Checks whether an element exists
size()Returns the number of elements
clear()Removes all elements
isEmpty()Checks whether the collection is empty
get()Retrieves an element by index from a List
put()Adds a key-value pair to a Map

Collection vs Array

CollectionArray
Size can generally change dynamically.Size is fixed after creation.
Provides many built-in methods.Has fewer built-in operations.
Stores objects using generic types.Can store primitives and objects.
Part of Java Collection Framework.Built into the Java language.

Advantages of Collections

  • Dynamic size.
  • Easy data manipulation.
  • Provides reusable data structures.
  • Supports searching and sorting.
  • Reduces programming effort.
  • Provides different structures for different requirements.

Key Points

  • The Collection Framework is mainly available in java.util.
  • List allows duplicates and maintains order.
  • Set does not allow duplicate elements.
  • Queue is useful for ordered processing.
  • Map stores data as key-value pairs.
  • Generics such as ArrayList<String> provide type safety.

Conclusion

The Java Collection Framework provides ready-made data structures for storing and manipulating groups of objects. List, Set, Queue, and Map are the major collection types, each designed for different data-management requirements.

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy