Better Programming

Advice for programmers.

Follow publication

What Are Java Collections? Get Started With the Framework

Implement the various data structures with Java

The Educative Team
Better Programming
Published in
8 min readMay 3, 2021

Java logo with data structures
Photo by the author.

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:

  • What are Java Collections?
  • ArrayList in Java
  • LinkedList in Java
  • HashSet in Java
  • TreeSet in Java
  • Other collections to study

What Are Java Collections?

According to the documentation, 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, while the classes that come under the Collection interface only store values.

Differences between the Collections framework and Collections

  • The Collections framework is an interface, while Collections is a class.
  • The Collection interface gives the standard functionality of data structures to List, Set, and Queue. The Collections class provides standard methods that you can use to search, sort, and coordinate collection elements.
  • Collections are objects that represent a group of objects (like Vector), while the Collections framework can represent and manipulate collections.

The Java Collections Framework Components

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:

  • Map interface: java.util.Map
  • Set interface: java.util.Set
  • Deque interface: 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:

Java Collections framework hierarchy
Java Collections framework hierarchy

ArrayList in Java

ArrayList is the most commonly used implementation of the List interface. Some of its features include:

  • Stores elements in insertion order.
  • Allows for storage of duplicate elements.
  • Supports null elements.

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:

  1. The no-arg constructor — This constructor doesn’t take any arguments and will create a list size of zero.
List list = newArrayList();

2. The constructor that takes an 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);

3. 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);

Here’s an example of an ArrayList you could execute in Java:

Output:

[1, 2, 3]The element at index two is 2The size of the List is 3

There are many things you can do with an ArrayList, including:

  • Update an element using the set(int index, E e) method.
  • Check if an element is present using the contains(Object o) method.
  • Remove all elements within a given Collection using the removeAll(Collection<?> c) method.
  • Replace all elements using the replaceAll(UnaryOperator<E> operator).
  • Etc.

LinkedList in Java

The LinkedList class implements the List and Deque interfaces. Some of its features include:

  • Stores elements in insertion order.
  • Supports duplicate elements.
  • Allows any number of null elements.

LinkedList also has a static inner class called Node that contains three fields:

  1. item — contains the value of the current element
  2. next — contains the pointer to the next element
  3. prev — 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:

  1. 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>();

2. 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);

Here’s an example of a LinkedList you could execute in Java:

Output:

[1, 2, 3][10, 1, 2, 3][10, 1, 20, 2, 3][10, 1, 20, 101, 102, 103, 2, 3]

Some more things you can do with LinkedList:

  • Fetch an element at a particular index using the get(int index) method.
  • Remove the last element using the removeLast() method.
  • Sort a LinkedList using the sort() method.
  • Etc.

HashSet in Java

HashSet is a class in the java.util package that implements the Set interface. Some of the features of the HashSet include:

  • Doesn’t allow duplicate elements.
  • Only allows one null element.
  • Elements are inserted in a random order.
  • Internally backed by a HashMap.

There are four ways to create a HashSet in Java:

  1. 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<>();

2. A constructor that takes an 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.

3. A constructor that takes an initial capacity and load factor — You can also provide an initial load factor along with an initial capacity.

4. 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.

Here’s an example of a HashSet you could execute in Java:

Output:

Inserting 17 in the HashSet: trueInserting 34 in the HashSet: trueInserting 17 in the HashSet: false[17, 34]

The code above 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 elements using the remove(Object o) method.
  • Check if the HashSet is empty using the isEmpty() method.
  • Fetch an element from a HashSet using the contains() method.
  • Etc.

TreeSet in Java

The TreeSet class implements the Set interface that uses a tree for storage. Some of the features include:

  • Doesn’t allow duplicate elements.
  • Doesn’t allow null elements.
  • Quick access and retrieval times.
  • Elements stored in ascending order.

The TreeSet hierarchy is as follows:

TreeSet hierarchy
TreeSet hierarchy

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:

  1. The no-arg constructor.
Set<Integer> set = new TreeSet<>();

2. 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. This will sort the elements per the logic of the Comparator.

3. A constructor with the Collection type argument — You can create a TreeSet from another Collection. The elements will be stored in ascending order.

4. 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.

Here’s an example of a TreeSet you could execute in Java:

Output:

TreeSet elements in ascending order [11, 21, 32, 44, 54]TreeSet elements in descending order [54, 44, 32, 21, 11]

There are more things you can do with TreeSet, such as:

  • Fetch elements greater than the given element using the tailSet(E fromElement) method.
  • Fetch a subset of elements using the subSet(E fromElement, E to Element) method.
  • Etc.

Other Collections To Study

Congratulations 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:

  • LinkedHashMap and LinkedHashSet
  • SortedMap and SortedSet
  • toArray()
  • hasNext()
  • Etc.

Happy learning!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

The Educative Team
The Educative Team

Written by The Educative Team

Master in-demand coding skills with Educative’s hands-on courses & tutorials.

No responses yet