Java Collections Framework

A collection represents a group of objects, known as its elements. This framework is provided in the java.util package. Objects can be stored, retrieved, and manipulated as elements of collections. Collection is a Java Interface. Collections can be used in various scenarios like Storing phone numbers, Employee names database etc. They are basically used to group multiple elements into a single unit. Some collections allow duplicate elements while others do not. Some collections are ordered and others are not.

A Collections Framework mainly contains the following 3 parts

A Collections Framework is defined by a set of interfaces, concrete class implementations for most of the interfaces and a set of standard utility methods and algorithms. In addition, the framework also provides several abstract implementations, which are designed to make it easier for you to create new and different implementations for handling collections of data.

Core Collection Interfaces

The core interfaces that define common functionality and allow collections to be manipulated independent of their implementation.
The 6 core Interfaces used in the Collection framework are:

  • Collection
  • Set
  • List
  • Iterator (Not a part of the Collections Framework)
  • SortedSet
  • Map
  • SortedMap

Note: Collection and Map are the two top-level interfaces.

Collection Interface

Map Interface

Concrete Classes

The concrete classes that are specific implementations of the core interfaces, providing data structures that a java program can use.

Note: Concrete Classes for the Map is shown in the previous section.
Standard utility methods and algorithms

Standard utility methods and algorithms

that can be used to perform various operations on collections, such as sorting, searching or creating customized collections.

How are Collections Used

  • The collections stores object references, rather than objects themselves. Hence primitive values cannot be stored in a collection directly. They need to be encapsulated (using wrapper classes) into an Object prior to storing them into a Collection (such as HashSet, HashMap etc).
  • The references are always stored as type Object. Thus, when you retrieve an element from a collection, you get an Object rather then the actual type of the collection stored in the database. Hence we need to downcast it to the Actual Type while retrieving an element from a collection.
  • One of the capabilities of the Collection Framework is to create a new Collection object and populate it with the contents of an existing Collection object of a same or different actual type.

Below is an example program showing the storing and retrieving of a few Collection Types

import java.util.*;

public class CollectionsDemo {

	public static void main(String[] args) {
		List a1 = new ArrayList();
		System.out.println(" ArrayList Elements");
		System.out.print("\t" + a1);
		List l1 = new LinkedList();
		System.out.println(" LinkedList Elements");
		System.out.print("\t" + l1);
		Set s1 = new HashSet(); // or new TreeSet() will order the elements;
		System.out.println(" Set Elements");
		System.out.print("\t" + s1);
		Map m1 = new HashMap(); // or new TreeMap() will order based on keys
		m1.put("Windows", "98");
		m1.put("Win", "XP");
		m1.put("Beginner", "Java");
		m1.put("Tutorial", "Site");
		System.out.println(" Map Elements");
		System.out.print("\t" + m1);


ArrayList Elements
[Beginner, Java, tutorial] LinkedList Elements
[Beginner, Java, tutorial] Set Elements
[tutorial, Beginner, Java] Map Elements
{Tutorial=Site, Windows=98, Win=XP, Beginner=Java}


Java Collections Source Code Examples

On the following pages in this tutorial I have described how elements can be manipulated by different collections namely;

Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.

Not readable? Change text. captcha txt