Java Tutorial: Collection, Map

By Xah Lee. Date:

This page teachs you about Java's Collection, Map, that's other language's list, array, vector, dictionary, hash table.

What's Collection, Map?

In Java, “collection” is basically a datatype that's list-like. In other languages called {list, array, vector, …}.

In Java, “map” is basically a datatype that's a set of key/value pairs. In other languages called {hash table, dictionary, map, associative list}.

In Java, the collection and map are actually Java Interfaces.

Note: “collection” and “map” are different interfaces, but sometimes the word “collection” informally mean both.

What's Interface?

A Java Interface defines a list of methods. Then, any Class can declare to implement that interface.

If a class C implements interface X and Y and Z, then it effectively means the C has all methods declared in X, Y, Z.

Java Interface can be inherited. That is, a interface Y can “extend” another interface X. So that, two interfaces may have a parent-child relationship. The parent interface is called “superinterface”. The child interface is called “subinterface”.

When a interface extends another, it basically modifies/adds more methods.

The full names are:

Take a look at the Java doc to get a clear picture:

if you want to know how to actually write code to define interface, or for a class to implement interface, see: What's Interface in Java?.

Sample methods of Collection Interface

The collection interface has methods like the following.

Collection (Java Platform SE 8 )

Sample Methods of Map Interface

A map interface has methods similar to the collection interface, but also has methods that get value of a given key. Remeber, a “map” is basically a list of key/value pairs.

Some sample methods of map interface:

Map (Java Platform SE 8 )

Children of Collection Interface

The Collection interface has the following children. Each of these interface is more specialized list-like type. (don't forget, these are all interfaces.)

Children of Map Interface

The Map interface has the following children:

Implementations of Collection Interface

The following are classes that implements the collection interface.

Implementations of Map Interface

Reference

The Collectdions Framework

Collections Framework Overview