Last Updated:

Java Collections Framework Overview

The Java Collections are one of the pillars of Java Core. They are used in almost every application, so we just have to be able to use the Java Collections Framework effectively.

What are Collections?

Collections are containers, groups of items that represent a cohesive whole.

For example: a candy can, a list of names, etc. Collections are used in almost every programming language and Java is no exception. As soon as collections appeared in Java, there were only a few classes: Vector, Stack, Hashtable, Array. But already in Java 1.2 there was a full-fledged Java Collections Framework, which we will get acquainted with today.

Collections in Java consist of several parts

  • Interfaces: In collections, interfaces provide an abstract data type for representing the collection— the root interface of the framework. It is at the top of the Collections hierarchy. It contains the most important methods: , , , , . Each collection must implement these methods. There are also other important interfaces and . is the only interface that does not inherit the interface, but is an integral part of the collections. All interfaces of the framework are in the .java.util.Collectionsize()iterator()add()remove()clear()java.util.List,java.util.Set,java.util.Queuejava.util.Map MapCollectionjava.util
  • Implementation: Java provides ready-made classes with an implementation of the above collections. We can use them to create new types of collections in our program. Classes can solve a huge number of problems, but if we need a special implementation of a particular collection, we can inherit it and work with our own implementation. Java 1.5 came up with thread-safe collections that allow you to change the contents of the collection while iterating through the elements. The most popular are: These classes are in the java.util.concurrent package. All collection classes are in packages and . ArrayList, LinkedList, HashMap, TreeMap, HashSet, TreeSetCopyOnWriteArrayList,ConcurrentHashMap,CopyOnWriteArraySet. java.utiljava.util.concurrent
  • Algorithms: Algorithms are useful methods that solve trivial problems, such as finding, sorting, and shuffling items in a collection.
  • The following class diagram shows the Hierarchy of the Java Collections Framework. For simplicity, I've only included frequently used interfaces and classes.
Java Collections Framework

Benefits of the Java Collections Framework

The Java Collections Framework has the following benefits:

  • Requires less effort. The framework has many common types of collections and useful methods for manipulating data. That way, we can focus on the business logic rather than developing our APIs.
  • Excellent quality – the use of well-tested collections increases the quality of our program.
  • Reuse and Compatibility

Collection interfaces

Interfaces are the foundation of the Java Collections Framework. Note that all interfaces are Generic, such as . Usage is specifying the type of object that a collection can contain. This helps reduce run-time errors by using compile-time object type checking.public interface Collection<E><E>

It should be noted that the Java platform does not provide separate interfaces for each type of collection. If an operation is not supported, the implementation of the collection drops UnsupportedOperationException.

Briefly for each collection

Iterator interface

The iterator provides methods for iterating through the elements of any collection. We can get an iterator instance from the collection using the . Iterators allow you to remove items from the base collection at iteration time.iterator

Set Interface

A set is a collection that cannot contain duplicate items. This interface represents a mathematical abstraction for representing sets as a deck of cards.

The Java platform contains three set: implementations, and the interface does not allow arbitrary access to an item in the collection. We can use an iterator or loop for each element to iterate through the elements.HashSet, TreeSetLinkedHashSet.ИSet

List interface

A list is an ordered set of items and can contain repeating items. You can access any item by index. The list is a dynamic array. A list is one of the most used collection types. and the classes are an implementation of the .ArrayListLinkedListList

Here is a small example of use:

Queue interface

A queue is a collection that is used to store multiple items.

In the queue, usually, but not necessarily, the elements are arranged according to the FIFO principle (first-i n, first-o ut = first entered, first exited). In the FIFO queue, all new items are inserted at the end of the queue.

Dequeue Interface

The Dequeue collection supports inserting an item and deleting an item at both the beginning and end of the collection. The name Deque is short for "two-ended queue" and is usually pronounced "deck". Most DEQUE implementations do not set limits on the number of elements.

This interface defines methods for accessing elements at the ends of the deck. Methods are provided for inserting, deleting, and retrieving an item.

Map Interface

A map is an object that contains keys and values. Map cannot contain duplicate keys: Each key can have only one value.

The Java platform contains three Map implementations: and HashMap, TreeMapLinkedHashMap.

ListIterator interface

The ListIterator allows a programmer to traverse a list in any direction, modify the list in an iteration, and get the iterator's current position in the list.

SortedSet interface

A sortedSet is a set in which items are stored in ascending order.

SortedMap interface

SortedMap contains elements in ascending order of keys. This Map is analogous to SortedSet. SortedMaps are used for naturally ordered key/value pairs, such as dictionaries and telephone directories.

Classes implementing Java collections

The Java Collections framework provides a large number of classes with an implementation of collection interfaces. The most used and common implementations are , and . Typically, classes that implement collections are not thread-safe.ArrayListHashMapHashSet

Later in this article, we'll look at the most used classes in the Java Collections framework.

HashSet Class

It is the basic implementation of the interface , which is based on .SetHashMap

This class offers the same execution time for basic . We can set the initial capacity and load factor for this collection.(add, remove, contains и size)

TreeSet class

NavigableSet Created from Items can be ordered in the order in which they are added or by using a comparator.TreeMap. 

This implementation provides log(n) execution time for basic operations ( and ).add, removecontains

ArrayList class

ArrayList is an implementation of the List interface as a variable-length array. Implements all list operations. In addition, arrayList provides methods for manipulating the size of the array that is used to store the list. (This class roughly corresponds to a vector, but is not .synchronized

LinkedList Class

LinkedList is an implementation of the List and Deque interfaces as a two-linked list. Performs all additional operations on the list.

HashMap class

HashMap is an implementation of the Map interface. This implementation provides all the additional Map operations and allows zero values and keys with a value of . The class is roughly equivalent to , except that it is out of sync and allows . This class does not provide any guarantee of the orderly placement of elements.nullHashMapHashtablenull

TreeMap class

TreeMap is a red-ebony tree based on NavigableMap. Maps are sorted using a comparator.

This implementation provides log(n) execution time for operations and .containsKey, get, putremove

PriorityQueue class

In the queue, items are added in FIFO order, but sometimes we want to add items based on their priority. In this case, we can use PriorityQueue while providing a comparator implementation for the PriorityQueue elements. Note that PriorityQueue does not allow you to store null

Collections Class

This class consists solely of static methods that work or return collections. It contains polymorphic algorithms that are used when working with collections.

This class contains the methods of the main algorithms of the Solarction framework, namely the methods of binary search, sorting, mixing, as well as a method that returns the reverse order of elements and many others.

Synchronized shells

Synchronized shells add automatic synchronization (thread-security) to a specific collection. Each of the six main collection interfaces (, and ) has a static factory synchronization method.Collection, Set, List, Map, SortedSetSortedMap

Each of these methods returns a synchronized (thread-safe) collection.

Immutable shells

Immutable shells/wrappers do not allow you to modify the collection, intercepting all operations that modify the collection and throw in the event that someone wants to do so. Here are these methods:

UnsupportedOperationException

Which collection to choose?

This question is often asked by programmers choosing between the many collections presented in the Java Collections Framework. Below is a table that contains all the most significant characteristics of the collections:

Collection

Ordering

Pricing

Random AccessKey-valueDuplicate Elements

Element

Zero

Potoko

security

ArrayListYesYesNoYesYesNo
LinkedListYesNoNoYesYesNo
HashSetNoNoNoNoYesNo
TreeSetYesNoNoNoNoNo
HashMapNoYesYesNoYesNo
TreeMapYesYesYesNoNoNo
VectorYesYesNoYesYesYes
HashtableNoYesYesNoNoYes
PropertiesNoYesYesNoNoYes
StackYesNoNoYesYesYes
CopyOnWriteArrayListYesYesNoYesYesYes
ConcurrentHashMapNoYesYesNoNoYes
CopyOnWriteArraySetNoNoNoNoYesYes

In this overview, we learned the main components of the Java Collections Framework. In the following articles, we'll break down popular collections and learn how to use each collection for its intended purpose.