Answer» The collection of objects refers to any group of individual objects that are represented as a single unit. In JDK 1.2, a new framework called “Collection Framework” was established, which contains all of the collection classes and interfaces. The Collection interface (present in the java.util.Collection package) and the Map interface (present in the java.util.Map package) are the two basic root interfaces of Java collection classes.
The above image shows various interfaces and the classes present in those interfaces in the Java Collections framework. The ones in yellow colour represent interfaces while the ones in blue colour represent classes that implement those interfaces. The following table lists the methods present in the collection interface:
| Method | Description |
|---|
| add(Object) | It is a method for adding an object to a collection. |
|---|
| addAll(Collection c) | This function populates this collection with all of the pieces from the provided collection. |
|---|
| clear() | This method clears the collection of all its items. |
|---|
| contains(Object o) | This method clears the collection of all its items. |
|---|
| containsAll(Collection c) | If the collection contains all of the elements in the given collection, this method returns true. |
|---|
| equals(Object o) | This method checks for equality between the specified object and this collection. |
|---|
| hashCode() | It is a method that returns the hash code value for a collection. |
|---|
| isEmpty() | It returns true if there are no elements in this collection. |
|---|
| iterator() | It returns an iterator that traverses the elements of this collection. |
|---|
| max() | The maximum value in the collection is returned by this method. |
|---|
| size() | The number of elements in the collection is returned by the size() method. |
|---|
| spliterator() | It is a function for creating a Spliterator over the elements in a collection. |
|---|
| stream() | This method returns a sequential Stream whose source is this collection. |
|---|
| toArray() | It returns an array that contains all of the elements in this collection. |
|---|
Interfaces that extends the Collections interface:- The following are the interfaces present in the Collection framework of Java:
Iterable Interface: This is the collection framework’s root interface. The iterable interface is extended by the collection interface. As a result, all interfaces and classes implement this interface by default. This interface’s main purpose is to provide an iterator for the collections. As a result, this interface only has one abstract method, the iterator.
Collection Interface: The Collection Interface extends the iterable interface and is implemented by all of the collection framework’s classes. This interface covers all of the basic methods that every collection has, such as adding data to the collection, removing data from the collection, clearing data, and so on. All of these methods are implemented in this interface since they are used by all classes, regardless of their implementation style. Furthermore, including these methods in this interface guarantees that the method names are consistent across all collections. In summary, we may conclude that this interface lays the groundwork for the implementation of collection classes.
List Interface: The collection interface has a child interface called the list interface. This interface is dedicated to list data, in which we can store all of the objects in an ordered collection. This also allows for the presence of redundant data. Various classes, such as ArrayList, Vector, Stack, and others, implement this list interface. We can create a list object with any of these classes because they all implement the list.
Java ArrayList: In Java, ArrayList allows us to create dynamic arrays. Though it may be slower than normal arrays, it might be useful in programs that require a lot of array manipulation. If the collection increases, the size of an ArrayList is automatically increased, and if the collection shrinks, the size of an ArrayList is automatically decreased. The Java ArrayList allows us to access the list at random. Primitive types, such as int, char, and so on, cannot be utilised with ArrayList. In such circumstances, we’ll require a wrapper class. Syntax - ArrayList<ObjectType>name=new ArrayList<ObjectType>(size_of_collection);
Java LinkedList: The LinkedList class implements the LinkedList data structure, which is a linear data structure with items not stored in contiguous locations with each element being a separate object having a data and address part. Pointers and addresses are used to connect the elements. Every element is referred to as a node. Syntax - LinkedList<ObjectType> name = new LinkedList<ObjectType>(size_of_collection);
Java Vector: In Java, a vector offers us dynamic arrays. Though it may be slower than normal arrays, it might be useful in programs that require a lot of array manipulation. In terms of implementation, this is similar to ArrayList. The main distinction between a vector and an ArrayList is that a vector is synchronised (i.e., thread-safe implying that it has some sort of locking mechanism before accessing shared data), but an ArrayList is not. Syntax - Vector<ObjectType> name = new Vector<ObjectType>(size_of_collection);
Java Stack: The Stack data structure is modelled and implemented by the Stack class. The class is organised around the last-in-first-out philosophy. The class also includes three additional functions: empty, search, and peek, in addition to the fundamental push and pop operations. The subclass of Vector can also be used to refer to this class. Syntax - Stack<ObjectType> name = new Stack<ObjectType>(size_of_collection);
Queue Interface: A queue interface, as the name implies, follows the FIFO (First In First Out) order of a real-world queue line. This interface is for storing all elements in which the order of the elements is important. When we try to book a ticket, for example, the tickets are sold on a first-come, first-served basis. As a result, the individual whose request is first in line receives the ticket. PriorityQueue, ArrayDeque, and other classes are available. Because all of these subclasses implement the queue, we can use any of them to create a queue object. Example - Queue<ObjectType> queue = new PriorityQueue<ObjectType> ();
Deque Interface: This is just a little tweak to the queue data structure. Deque, also known as a double-ended queue, is a data structure in which elements can be added and removed from both ends. The queue interface is extended by this interface. ArrayDeque is the class that implements this interface. Because the ArrayDeque class implements the Deque interface, we can use it to create a deque object. Example - Deque<ObjectType> deque = new ArrayDeque<ObjectType> ();
Set Interface: A set is an unordered collection of objects in which no duplicate values can be stored. This collection is utilised when we want to avoid duplication of things and only keep the unique ones. Various classes, such as HashSet, TreeSet, LinkedHashSet, and others, implement this set interface. We can create a set object with any of these classes because they all implement the set. Example - Set<ObjectType> set = new HashSet<ObjectType> ();
SortedSet Interface: This interface resembles the set interface in appearance. The only difference is that this interface provides additional methods for maintaining element ordering. The sorted set interface is an extension of the set interface that is used to manage sorted data. TreeSet is the class that implements this interface. We can create a SortedSet object using this class because it implements the SortedSet interface. Example - SortedSet<ObjectType> set = new TreeSet<ObjectType> ();
Map Interface: A map is a data structure that allows data to be mapped into key-value pairs. Because the same key cannot have multiple mappings, this interface does not support duplicate keys. If there is data and we want to do actions based on the key, a map is beneficial. Various classes, such as HashMap and TreeMap, implement this map interface. We can create a map object with any of these classes because they all implement the map. Example - Map<ObjectType, ObjectType> map_name = new HashMap<ObjectType, ObjectType>();
|