Table of contents
1.
Introduction
2.
Collection Class Declaration
3.
Java Collection Classes
3.1.
1. ArrayList
3.2.
2. Vector
3.3.
3. Stack
3.4.
4. LinkedList
3.5.
5. HashSet
3.6.
6. LinkedHashSet
3.7.
7. TreeSet
3.8.
8. PriorityQueue
3.9.
9. ArrayDeque
3.10.
10. HashMap
3.11.
11. EnumMap
3.12.
12. AbstractMap
3.13.
13. TreeMap
4.
Java Collections Class Fields
5.
Java Collections Examples
5.1.
Adding Elements to the Collections
5.2.
Sorting a Collection
5.3.
Searching in a Collection: 
5.4.
Copying Elements 
5.5.
Disjoint Collection 
6.
Frequently Asked Questions
6.1.
What is the difference between ArrayList and LinkedList in Java?
6.2.
Can we use primitive data types with Java Collections?
6.3.
What is the purpose of the Collections.synchronizedCollection() method?
7.
Conclusion
Last Updated: Jan 9, 2025
Easy

Collections Class in Java

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java is a popular programming language which is used for developing a wide range of applications. One of the key features of Java which makes it popular is its extensive collection framework, which provides a set of classes & interfaces for storing & manipulating groups of objects. The Java Collections framework is designed to simplify the development process by providing reusable components that can be easily integrated into your code.

Collections Class in Java

In this article, we will talk about the Java Collections class, its declaration, and various classes within the collection framework. 

Collection Class Declaration

In Java, the Collection interface is the foundation on which the Collections Framework is built. It provides the standard methods that all collections will have. This interface is part of the java.util package, which makes it necessary to import this package when working with collections in Java.

The basic syntax to declare a collection in Java is :

import java.util.*;


Collection<Type> collectionName = new ArrayList<Type>();


In this example, Type represents the type of objects that the collection will store, and ArrayList is one of the many classes that implement the Collection interface.

Note: This setup is essential for manipulating groups of objects, allowing for operations such as addition, deletion, and traversal of elements in a uniform manner across different types of collections.

Java Collection Classes

The Java Collections Framework consists of many classes that implement the Collection interface, each designed for specific use cases and characteristics. 

Let’s discuss few of them : 

1. ArrayList

ArrayList provides resizable-array implementation of the List interface. It allows for dynamic arrays that can grow as needed.


Syntax:

List<String> list = new ArrayList<>();

2. Vector

Similar to ArrayList, Vector implements a dynamic array that can grow or shrink as required. However, unlike ArrayList, it is synchronized, making it thread-safe.


Syntax:

Vector<Integer> vector = new Vector<>();

3. Stack

Stack class represents a last-in-first-out (LIFO) stack of objects. It extends Vector with five operations that allow a vector to be treated as a stack.


Syntax:

Stack<Double> stack = new Stack<>();

4. LinkedList

LinkedList implements both List and Deque interfaces, allowing it to be used both as a list and as a queue. It is ideal for applications with frequent insertions and deletions.


Syntax:

LinkedList<Character> linkedList = new LinkedList<>();

5. HashSet

HashSet is used to create a collection that uses a hash table for storage. It exemplifies the Set interface, guaranteeing unique elements.

Syntax:

Set<Long> hashSet = new HashSet<>();

6. LinkedHashSet

LinkedHashSet maintains a linked list of the entries in the set, in the order in which they were inserted. This allows insertion-order iteration over the set.


Syntax:

LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>();

7. TreeSet

TreeSet is a NavigableSet implementation that uses a Red-Black tree architecture. It orders its elements based on their values.


Syntax:

TreeSet<String> treeSet = new TreeSet<>();

8. PriorityQueue

PriorityQueue class is a priority queue based on the priority heap. Elements of the priority queue are ordered according to their natural ordering.


Syntax:

PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();

9. ArrayDeque

ArrayDeque class provides a way to apply resizable-array implementation of the Deque interface. It is faster than Stack and LinkedList as a stack and queue.


Syntax:

ArrayDeque<String> arrayDeque = new ArrayDeque<>();

10. HashMap

HashMap is a part of Java’s collection framework and provides the basic implementation of the Map interface of Java. It stores data in key-value pairs.


Syntax:

HashMap<String, Integer> hashMap = new HashMap<>();

11. EnumMap

EnumMap is a specialized Map implementation for use with enum keys. It is compact and efficient.


Syntax:

EnumMap<MyEnum, String> enumMap = new EnumMap<>(MyEnum.class);

12. AbstractMap

This class provides a skeletal implementation of the Map interface, to minimize the effort required to implement this interface.


Syntax:

AbstractMap<String, Integer> abstractMap = new HashMap<>();

13. TreeMap

TreeMap is a map implemented by a Red-Black tree. It stores keys in a sorted order, and it is part of the Java Collections Framework.


Syntax:

TreeMap<String, Integer> treeMap = new TreeMap<>();

Java Collections Class Fields

When working with the Java Collections Framework, it's important to understand the typical fields associated with these classes, which help in managing the collection's state and behavior. Let’s see some of the common fields found in collection classes:

1. Capacity- This field typically applies to classes like ArrayList and Vector, where it refers to the size of the array used to store the elements in the list. It adjusts automatically as elements are added or removed.

2. Size- The size field indicates the number of elements currently stored in the collection. This is applicable across all collection types.

3. Comparator- Used in sorted collections like TreeSet and TreeMap, this field defines a comparator that is used to order the elements in the collection.

4. Load Factor- Found in hash table based collections like HashSet and HashMap, the load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased.

5. Mod Count- This field is used internally in many collections to ensure the integrity of iterators. It counts the number of times a collection has been structurally modified.

Java Collections Examples

Adding Elements to the Collections

ArrayList Example:
List<String> names = new ArrayList<>();
names.add("Rahul");
names.add("Rinki");
names.add("Harsh");
System.out.println("Names List: " + names);
You can also try this code with Online Java Compiler
Run Code

Output:

Names List: [Rahul, Rinki, Harsh]

Sorting a Collection

TreeSet Example:
TreeSet<Integer> numbers = new TreeSet<>();
numbers.add(5);
numbers.add(1);
numbers.add(3);
System.out.println("Sorted Numbers: " + numbers);
You can also try this code with Online Java Compiler
Run Code

Output:

Sorted Numbers: [1, 3, 5]

Searching in a Collection: 

HashSet Example

Set<String> items = new HashSet<>();
items.add("pen");
items.add("notebook");
items.add("eraser");
boolean hasNotebook = items.contains("notebook");
System.out.println("Contains notebook: " + hasNotebook);
You can also try this code with Online Java Compiler
Run Code

Output:

Contains notebook: true

Copying Elements 

Copying from one List to another:

List<Integer> original = new ArrayList<>(Arrays.asList(1, 2, 3));
List<Integer> copy = new ArrayList<>(original);
System.out.println("Copied List: " + copy);
You can also try this code with Online Java Compiler
Run Code

Output

Copied List: [1, 2, 3]

Disjoint Collection 

Check if two collections have no elements in common:

List<String> group1 = Arrays.asList("apple", "banana", "cherry");
List<String> group2 = Arrays.asList("dog", "cat", "bird");
boolean isDisjoint = Collections.disjoint(group1, group2);
System.out.println("Collections are disjoint: " + isDisjoint);
You can also try this code with Online Java Compiler
Run Code

Output

Collections are disjoint: true

Frequently Asked Questions

What is the difference between ArrayList and LinkedList in Java?

ArrayList is implemented as a dynamic array, while LinkedList is a doubly-linked list. ArrayList provides faster access to elements, but LinkedList offers better performance for insertion and deletion operations, especially at the beginning or middle of the list.

Can we use primitive data types with Java Collections?

No, Java Collections only work with object types. However, you can use wrapper classes like Integer, Double, or Boolean to store primitive values in collections.

What is the purpose of the Collections.synchronizedCollection() method?

The Collections.synchronizedCollection() method is used to create a thread-safe collection by wrapping the specified collection with a synchronized wrapper. This allows multiple threads to safely access the collection concurrently.

Conclusion

In this article, we have learned about the Java Collections class and its various components. We explained different collection classes like ArrayList, Vector, Stack, LinkedList, HashSet, LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, HashMap, EnumMap, AbstractMap, and TreeMap. We also looked at the syntax and examples for each of these classes.

Live masterclass