In Java, the collection hierarchy is a set of interfaces and classes that define different types of collections and their behaviors. The three main interfaces in the collection hierarchy are List
, Set
, and Map
.
- List: The
List
interface represents an ordered collection of elements that allows duplicates. Some commonly used implementations of theList
interface areArrayList
andLinkedList
. TheList
interface extends theCollection
interface and provides additional methods to access and manipulate elements by their index. Elements in a list can be accessed in the order they were inserted. - Set: The
Set
interface represents a collection of unique elements with no particular order. It does not allow duplicate elements. Some commonly used implementations of theSet
interface areHashSet
andTreeSet
. TheSet
interface extends theCollection
interface and provides methods for adding, removing, and checking the presence of elements. - Map: The
Map
interface represents a mapping between keys and values. Each key in aMap
must be unique, and it is associated with a corresponding value. Some commonly used implementations of theMap
interface areHashMap
andTreeMap
. TheMap
interface does not extend theCollection
interface, but it provides methods to manipulate the key-value pairs, such as adding, removing, and retrieving values based on their keys.
Here’s a graphical representation of the collection hierarchy in Java:
Collection (Interface)
|
________________________________
| |
List (Interface) Set (Interface)
| |
ArrayList HashSet (Class)
LinkedList TreeSet (Class)
Map (Interface)
|
HashMap (Class)
TreeMap (Class)
In addition to these core interfaces and classes, there are other specialized implementations and interfaces available in the Java Collections Framework, such as Queue
, Deque
, and SortedSet
. These provide additional functionality and behavior for specific use cases.