Witscale Test Center

14.2 Six core interfaces


14.2 Six core interfaces

The collection framework has six interfaces that generically describe the behavior of different types of collection. Figure 14.2 shows the six interfaces and their relationship among each other.

 


Figure 14.2 The six core interfaces in the collection framework

Figure shows two interface hierarchies. The Collection interface is extended to define specialized interfaces List and Set. Set is further extended to define SortedSet interface. The Map interface is independent of Collection interface. It is extended to define SortedMap interface. Let us quickly review these six interfaces and their purpose.

java.util.Collection

This is the generalized interface for maintaining collections. It describes the generic behavior for adding, removing elements in the collection. This interface is the super interface for more specialized interfaces such as Set and List.  No concrete top-level class in java.util directly implements this interface.

java.util.Set

The Set interface extends the Collection interface. Mathematically a set is a group of all unique elements. Similarly, the Set interface describes a collection that has all unique elements. In other words, no duplicate elements are allowed in a collection that implements this interface.

java.util.List

The List interface also extends Collection interface. Unlike set, a list defines a collection that need not have all unique elements. In other words, there can be duplicate elements (that means same object can occur multiple times) in a list. Also the List interface describes a collection that can maintain order in which the elements were inserted. In simple words, a list is an ordered collection that can have duplicate elements.

java.util.SortedSet

The SortedSet interface extends from the Set interface to add the sorting functionality. This interface describes behavior of collection in which no duplicates are allowed and elements are kept in sorted order. For instance, if you are storing strings in a collection that implements SortedSet, they are stored in alphabetically ascending order.

java.util.Map

A Map describes behavior of collection of elements in which each element is identifiable by unique key. In other words, it describes a group of mappings in the form of key-and-value pairs. Conceptually a Map is not a collection as it stores the mappings (or associations) instead of actual elements. For that reason, this interface is not extended from the Collection interface.

java.util.SortedMap

A SortedMap describes behavior of collection of key-and-value pairs where these pairs are sorted as per the key. This interface extends from the Map interface.

In the next section, we will learn more about each of these interfaces by looking at the examples of classes that implements them.