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
interface represents an ordered collection of elements that allows duplicates. Some commonly used implementations of the List
interface are ArrayList
and LinkedList
. The List
interface extends the Collection
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
interface represents a collection of unique elements with no particular order. It does not allow duplicate elements. Some commonly used implementations of the Set
interface are HashSet
and TreeSet
. The Set
interface extends the Collection
interface and provides methods for adding, removing, and checking the presence of elements.Map
interface represents a mapping between keys and values. Each key in a Map
must be unique, and it is associated with a corresponding value. Some commonly used implementations of the Map
interface are HashMap
and TreeMap
. The Map
interface does not extend the Collection
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.
Copyright ©TechOceanhub All Rights Reserved.