Note: This post was originally published in 2021 and has been updated as of Apr. 7, 2022.
Collections are used in every programming language. They are objects that group multiple elements into a single unit. Before the Collections Framework, it was hard for programmers to write algorithms that worked for different kinds of collections. Java came with some Collection classes, like Vector, Stack, Hashtable, and Array, but they had their disadvantages.
In JDK 1.2, Java developers introduced the Collections Framework, which is an important framework to help you achieve all of your data operations. Today, we’ll dive deeper into this framework and some of its components.
Here’s what we’ll cover today:
Try one of our 300+ courses and learning paths: Collections in Java.
The Java Collections Framework is a unified architecture for representing and manipulating collections. It contains interfaces, their implementation classes, and algorithms to process the data stored in a collection. The Collection interface is extended by other interfaces like List, Set, and Queue.
Note: There’s also a Map interface, but it doesn’t implement the Collection interface because it stores key-value pairs, and the classes that come under the Collection interface only store values.
Differences between the Collections Framework and Collections:
Interfaces: These interfaces supply the abstract data type to represent the collection. The java.util.Collection
is the root interface of the framework. It’s at the top of the framework hierarchy and contains important methods, like size()
, iterator()
, add()
, remove()
, and clear()
.
The iterable interface is the root of the whole collection framework. It allows the iterator to iterate through all of the collections. All classes and interfaces use this interface. The collection interface extends the iterable interface and is executed by the classes in the collection framework. The list interface inhibits a list type data structure where we can store ordered collections of objects.
Some more important interfaces include:
java.util.Map
java.util.Set
java.util.Deque
Note: The Map interface is the only one that doesn’t inherit from the Collection interface, but is included in the Collections Framework. All framework interfaces are in the java.util package.
Implementation classes: The framework provides implementation classes for collections. You can use them to create different types of collections in your Java programs. Some of the main collection classes include:
ArrayList
LinkedList
PriorityQueue
HashMap
and HashSet
TreeMap
and TreeSet
Algorithms: These algorithms perform important functions on collections, like sorting lists.
The Collections Framework hierarchy is as follows:
The ArrayList in Java is the most commonly used implementation of the List interface. Some of its features include:
An ArrayList stores data in a resizable array. When you create an ArrayList, you create an array of size zero. When the first element is inserted, the array size changes to ten. This is known as lazy initialization and it saves a lot of memory. Before adding an element to an ArrayList, the capacity is checked. If the array is full, then a new array of size (n + n/2 + 1) is created. The elements from the old array are then copied to the new one.
There are three ways to create an ArrayList in Java:
The no-arg constructor
This constructor doesn’t take any arguments and will create a list size of zero.
List list = newArrayList();
The constructor that takes initial capacity
You can give an initial capacity when creating an ArrayList. Let’s say you know that your ArrayList will contain a minimum of 50 elements. You can create your ArrayList with a size of 50, reducing the need for constant resizing.
List list = newArrayList(50);
Existing Collection
You can create an ArrayList using an existing Collection, and the list will contain all the elements in the original collection in the same order.
List list = newArrayList(oldList);
Let’s look at an execution of ArrayList in Java:
import java.util.ArrayList;import java.util.List;public class ArrayListDemo {public static void main(String args[]) {List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);System.out.println(list);System.out.println("The element at index two is " + list.get(1));System.out.println("The size of the List is " + list.size());}}
There are many things you can do with an ArrayList, including:
set(int index, E e)
methodcontains(Object o)
methodremoveAll(Collection<?> c)
methodreplaceAll(UnaryOperator<E> operator)
The LinkedList class implements the Linked List data structure in Java. Some of its features include:
LinkedList also has a static inner class called Node
, which contains three fields:
item
: contains the value of the current elementnext
: contains the pointer to the next elementprev
: contains the pointer to the previous element]When an element is added to a LinkedList, it creates a new Node
instance. The prev and next fields are set depending on where the new node is added.
There are two ways to create a LinkedList in Java:
The no-arg constructor
This constructor doesn’t take any arguments and will create a list size of zero.
List<Integer> list = new LinkedList<Integer>();
Existing Collection
You can create a LinkedList using an existing Collection, and the list will contain all the elements in the original collection in the same order.
List<Integer> list = new LinkedList<Integer>(oldList);
Let’s look at an execution of LinkedList in Java:
import java.util.ArrayList;import java.util.LinkedList;import java.util.List;public class LinkedListDemo {public static void main(String args[]) {LinkedList<Integer> linkedList = new LinkedList<>();linkedList.add(1); // Adds 1 to the list.linkedList.add(2); // Adds 2 to the end of the list.linkedList.addLast(3); // Adds 3 to the end of the list.System.out.println(linkedList);linkedList.addFirst(10); // Adds 10 to the start of the list.System.out.println(linkedList);linkedList.add(2, 20); // Adds 20 to second position in the list.System.out.println(linkedList);List<Integer> list = new ArrayList<>();list.add(101);list.add(102);list.add(103);linkedList.addAll(3, list); // Adds the collection of elements at third position in the list.System.out.println(linkedList);}}
Some more things you can do with LinkedList include:
get(int index)
methodremoveLast()
methodsort()
method
Try one of our 300+ courses and learning paths: Collections in Java.
HashSet is a class in the java.util
package which implements the Set interface. Some of the features of the HashSet include:
There are four ways to create a HashSet in Java:
The no-arg constructor
This will create a HashSet with an initial capacity of 16 and a load factor of 0.75.
Set<Integer> set= new HashSet<>();
A constructor that takes initial capacity
If you know your HashSet will have more than 16 elements, you can set a higher initial capacity to reduce the need for resizing. It will use a default load factor of 0.75.
A constructor that takes initial capacity and load factor
You can also provide an initial load factor along with an initial capacity.
A constructor that takes another Set as a parameter
You can create a HashSet using another Set by passing it to the constructor. It will have the same size as the passed set and a default load factor of 0.75.
Let’s look at an execution of a HashSet in Java:
import java.util.HashSet;import java.util.Set;public class HashSetDemo {public static void main(String args[]) {Set<Integer> set = new HashSet<>();System.out.println("Inserting 17 in the HashSet: " + set.add(17));System.out.println("Inserting 34 in the HashSet: " + set.add(34));System.out.println("Inserting 17 in the HashSet: " + set.add(17));System.out.println(set);}}
The above code showed a demonstration of the add(E e)
method that inserts an element into the HashSet. If the inserted element wasn’t already in the HashSet, the method returned true
. If the element was already in the HashSet, the method returned false
.
There’s more you can do with HashSets, like:
remove(Object o)
methodisEmpty()
methodcontains()
methodThe TreeSet class implements the Set interface that uses a tree data structure for storage. Some of the features include:
The TreeSet hierarchy is as follows:
How is a TreeSet different from a HashSet?
- HashSet allows one null element, while TreeSet doesn’t allow any
- Elements are sorted randomly in HashSet, while they are sorted in order in TreeSet
- HashSet is faster for operations like add, remove, contains, size, etc.
There are four ways to create a TreeSet in Java:
Set<Integer> set = new TreeSet<>();
A constructor with Comparator as an argument
If the objects you store in your TreeSet don’t implement the Comparator interface or if you need to store the elements in descending order, you can provide a custom Comparator while creating a TreeSet, which will sort the elements per the logic of the Comparator.
A constructor with the Collection type argument
You can create a TreeSet from another Collection. The elements will be stored in ascending order.
A constructor with the argument of type SortedSet
This constructor acts as a copy constructor and creates a new sorted set with the same elements and the same order of the provided sorted set.
Now, let’s look at an execution of TreeSet:
import java.util.Comparator;import java.util.TreeSet;public class TreeSetDemo {public static void main(String args[]) {TreeSet<Integer> set = new TreeSet<>();set.add(21);set.add(32);set.add(44);set.add(11);set.add(54);System.out.println("TreeSet elements in ascending order " + set);// This TreeSet will store the elements in reverse order.TreeSet<Integer> reverseSet = new TreeSet<>(Comparator.reverseOrder());reverseSet.add(21);reverseSet.add(32);reverseSet.add(44);reverseSet.add(11);reverseSet.add(54);System.out.println("TreeSet elements in descending order " + reverseSet);}}
There are more things you can do with TreeSet, such as:
tailSet(E fromElement)
methodsubSet(E fromElement, E to Element)
methodCongratulations on taking your first steps with the Java Collections Framework! Collection is one of the most important topics in Java programming, and this important framework will help you accomplish all of your data operations.
You’re now ready to dive deeper into the Java Collections Framework and learn about more topics such as:
toArray()
hasNext()
To get started learning more about the Java Collections Framework, check out Educative’s course Collections in Java. This curated, hands-on course covers all the collection types available in the Collections Framework. You’ll cover the internal workings of each Java collection so you can work with them more efficiently.
The skills you gain in this course will give you an edge in Java-based interviews.
Happy learning!
Free Resources