StackTips

Introduction to Java Collections

siteadmin avtar

Written by

Stacktips,  5 min read,  305 views, updated on July 24, 2024

Java Collections API is a set of interfaces and implementations are included in the Java standard library. The collections framework in Java defines different data structures in which you can store, group, and retrieve objects.

There are different collection implementations with their distinct behaviours and purpose. All collection interfaces and classes are available under java.util package.

In this lesson, we'll dive into some of these collections and learn how to do the basic operations in each.

Collection Interface

The collection interface is the root of the collection hierarchy and it represents general-purpose behavior for the entire interface in this hierarchy. It doesn’t have a concrete implementation.


List Interface

A list is an ordered collection of elements that may contain duplicates. The indexing of the elements is similar to Array, and the first index of the List is zero.

The elements can be manipulated based on their indexed position. The common List implementations in Java are:

Set Interface

A set is a collection that doesn’t allow duplicate entries. A set has the same methods as the List interface. It can contain at most one 1 null element.

The common Set implementations in Java include:

Queue Interface

A Queue represents a collection where elements are inserted at the back and removed from the front (FIFO - First In, First Out).

The common Queue implementations include:

Map Interface

The Map interface in Java is not a collection but it is part of the Collection framework. It implements the Map interface and can store key-value pairs.


The common Map implementations include:

  • HashMap: Unordered, allows one null key and multiple null values.
  • LinkedHashMap: Ordered by insertion, allows one null key and multiple null values.
  • IdentityHashMap: Uses reference equality for keys.
  • TreeMap: Sorted order, does not allow null keys, allows multiple null values.
  • Hashtable: Synchronized, does not allow null keys or values.

Getting Started with Java - Learn Java For Free

Java is a high-level, cross-platform, mature, object-oriented programming language. Java is extremely popular due to its flexibility and ability to run anywhere!

>> CHECK OUT THE COURSE

Continue reading..