Updated April 17, 2023
Introduction to java collection hierarchy
Java Collection Hierarchy is the hierarchy of the whole collection framework and it contained within it with four core interfaces they are Collection, Set, Map and List. Additionally, there are two more focused interfaces namely the SortedSet and SortedMap which is used for sorting purpose. The entire interfaces and the classes for the collection hierarchy framework where placed in java.util.package.
What is Java Collection Hierarchy?
The Java Collection hierarchy in java is a framework provides the structural design which is used to store up and control the group of objects. The Java collection hierarchy accomplished with the entire operations performed on data like insertion, deletion, searching, sorting and manipulation. The Java Collection depicts the single unit of objects. It makes available with a number of interfaces like Set, Queue, Deque, List and also it available with the classes like ArrayList, LinkedList, LinkedHashSet, HashSet,
PriorityQueue, Vector and TreeSet. Additionally, there are two more fixed interfaces namely the SortedSet and SortedMap for sorting purpose. The entire interfaces and the classes for the collection hierarchy framework where placed in java.util.package.
Interface Java Collection Hierarchy Methods
In Java Collection Hierarchy, there are several Collection Interface Methods which is used for controlling elements in the collection. Let’s see the following Java Collection Interface Methods,
- add(): This method is mainly used for inserting or adding an element in the collection. In the name itself it depicts that add, to add an element to the collection. When adding a new element to the collection it returns TRUE otherwise it returns FALSE. It avoids duplications so if the particular added element present already the collection does not accept it. The common syntax used for add() method is as follows,
Syntax: add (Object Element): Boolean
- addAll(): This addAll() is used to add all the elements in the specified collection to this collection. When adding a new element to the collection it returns TRUE otherwise it returns FALSE. The common syntax used for addAll() method is as follows,
Syntax: addAll(Collection c) : Boolean
- Clear(): this clear() method is used to remove or clear an element in the collection. It does not return anything. The common syntax used for Clear() method is as follows,
Syntax: clear() : void
- Contains(): This method is used to check whether the specific element is present in the collection or not, mainly it is used for searching/ finding an element. It returns TRUE if it contains the specific element in the collection otherwise it returns FALSE. The common syntax used for contains() method is as follows,
Syntax: contains (Object element) : Boolean
- ContainsAll(): This method is used to check whether the entire element is present in the given collection or not. It returns TRUE if it contains all element in the collection otherwise it returns FALSE. The common syntax used for containsAll() method is as follows,
Syntax: containsAll(Collection c) : Boolean
- equals(): This method is used to check the equality with another object, it compares the particular object in the collection for equality. The common syntax used for equal() method is as follows,
Syntax: equal(Object element) : Boolean
- isEmpty(): This method is used to return true only when the collection is empty, otherwise it returns false. The common syntax used for isEmpty() method is as follows,
Syntax: isEmpty() : Boolean
- iterator(): This method is used to return an iterator over the elements in the collection. The common syntax used for iterator() method is as follows,
Syntax: iterator() : iterator
- remove(): This method is used to return an Boolean value, if it removes the element it returns true otherwise it returns false. It removes the given particular element from the collection. If there is a duplicate value, then it removes the first occurrence of the object. The common syntax used for remove() method is as follows,
Syntax: remove(Object element): Boolean
- removeAll(): This method is used to return an Boolean value, if it removes all the element it returns true otherwise it returns false. It removes the entire element from the collection. The common syntax used for removeAll() method is as follows,
Syntax: removeAll (Collection c): Boolean
- retainAll(): This method is used to retains the elements which are only in the collection contained in particular collection. It returns a Boolean value. The common syntax used for retainAll() method is as follows,
Syntax: retainAll(Collection c): Boolean
- size(): This method is used to return the number of elements appears in the collection. The Size() method return type is integer it returns the total number of elements in the collection. The common syntax used for size() method is as follows,
Syntax: size(): int
- toArray(): This method is used to return the array contained elements in the collection. It returns in the form of an array elements in the collection. The common syntax used for toArray() method is as follows,
Syntax: toArray(): Object [ ]
- Object[ ] toArray(): This method is used to return the array contained in the elements stored in invoked collection. The common syntax used for Object toArray() method is as follows,
Syntax: toArray (Object array[ ]): Object [ ]
Java Collection Hierarchy Methods
The Java Collection Hierarchy classes implements the cores interfaces and also make available with reliable functionalities. Let’s see the following methods,
- Collection – Collection Interface is the basic root hierarchy. It provides with the entire methods which all the collections class supports.
- List – The List represent the collection of elements in ordered way. By using the List, access the elements by their index and also we can search for an element.
- Set – The Set represents the collection of elements in sorted way.
- Map – The Map interface make available with three collections we can viewed as set of keys, collection of values and set of key-value mapping way. It enables to store data in key-value pairs.
- Stack – it represent the elements in stack data structures which we can pushed to LIFO (Last-In-First-Out) method of objects.
- Queue – it is a queue data structure which is projected to hold an element prior to process by user thread. Queue provides the insertion, inspection and extraction operations.
- Deque – it is a double-ended-queue which provides the element in insertion and removal at both the ends. This interface preferably used the stack class.
Java Collection Hierarchy Class Diagrams
Let’s see the class diagram of Java Collection Hierarchy, it contained in it with four core interfaces they are Collection, Set, Map and List. Additionally there are two more focused interfaces namely the SortedSet and SortedMap which is used for sorting purpose. The entire interfaces and the classes for the collection hierarchy framework where placed in java.util.package.
<image>
Blue box represents the Interface
Red Box represents the Class
E➝ depicts the extends, which is a keyword used for building interface between two classes and two interfaces.
I➝ depicts the implements, which is a keyword used for building the inheritance between the class and the interface.
Conclusion
Hope the article helps to understand the Java Collection Hierarchy, it gives the user to access the data structures and also to algorithms for operating them. It is the collection of object which holds the references to another object. The interface and the classes are packaged in java.util.
Recommended Articles
This is a guide to java collection hierarchy. Here we discuss the Java Collection Hierarchy, which gives the user to access the data structures. You may also have a look at the following articles to learn more –