In this tutorial, we are going to discuss the Java Collection Framework and the key interfaces in it.
A Collection is a framework in Java which provides the architectures to store and manipulate a group of objects.
Hence, all the operations that we like to perform on any data like searching, sorting, manipulation, insertion, deletion, etc. can also be performed by the Java Collections.
Collection interface provide highly efficient implementations that allow you to perform operations on collections. So, by using these implementations, you can take advantage of standard methods in Java instead of writing a lengthy code.
Advantages of the Collection Framework:
- The framework will give high performance.
- Implementations of the highly efficient fundamental collections such as dynamic Arrays, Trees, LinkedList and Hashtable.
- The framework allows many different types of collections with high degree interoperability.
- Extending and adapting the Framework collection is easy.
What is Collection Framework in Java?
A Collection represents a single unit of objects such as a group. The other name of the collection is container. Typically, collections represent the data items. For example, coins collection (All the coins at one place) and mailbox(Collection of mails).
The Collections Framework contains a huge number of classes and interfaces that provides the functionality to manage collections.
Collection framework always represents a combined architecture for storing the objects. Collection Framework has some of the techniques such as
What is a Collection Interface?
A collection is a generic interface which provides all the fundamental methods required to work on collections. Below is the basic collection framework interface hierarchy.
Methods in the Collection Interface
The most commonly used methods in the Collection interface are:
int size(): Returns the number of elements in a collection.
boolean isEmpty(): Identifies whether a collection is empty. Returns true if the collection does not contain any element or else returns false.
boolean contains(Object element): Checks whether the specified element is a part of the collection. Returns true if the element is found or else returns false.
boolean add(E element): Adds the specified element to the collection. The type of the element must be compatible with the type of collection. Returns true if the element is successfully added to the collection. Returns false if the element is already found in the collection and the collection does not allow duplicates.
boolean remove(Object element): Removes one occurrence of the specified element from the collection. Returns true if the element is successfully removed or else returns false.
Iterator<E> iterator(): Provides an iterator for the collection.
Also, some methods perform bulk operations on the entire collection, such as
equals() is another important method which compares two collections.
In addition, there is a conversion constructor in the Collection interface that allows initialization of new collections with the contents of another collection.
List<int> lst1 = new ArrayList<int> c;
where c is another collection containing elements of int type.
The List Interface
The List interface provides the functionality to store and manage a sequence of items. It extends the Collection interface. Some of the significant features of the List interface include:
The index value of the elements starts with 0.
- Ability to access(read) or add(insert) elements at specific positions in a list using the appropriate index value of those positions.
Ability to insert duplicate elements into a list.
- Lists are mutable.
Availability of methods from the Collection interface and additional methods to manage lists.
Methods in the List Interface
The most commonly used methods in the List interface are:
void add(int index, E obj):
Inserts the object obj at the location specified by index. If any element is already present at that location, the list elements are shifted.
E get(int index):
Returns the object at the location specified by index.
E set(int index, E obj):
Assigns the object obj to the specified location and returns the old object at that location.
int indexOf(Object obj):
Identifies the first occurrence of obj in the collection and returns its location. If the element obj is not found, -1 is returned.
The Set Interface
The Set interface provides the functionality to store and manage a set of elements that does not allow duplicates. It is a generic interface that extends the Collection interface and does not provide any additional methods. Some of the key characteristics of the Set interface include:
- Immutability of a set. (We cannot change the elements)
Capability to restrict duplicate elements. The add()method returns false if an attempt is made to insert a duplicate element.
Ability to compare the contents of two sets accurately. Because of the restriction on duplicates, when two set instances are compared using the equals() method, a true value is returned only if all the elements are equal.
The SortedSet interface extends the Set interface and adds methods to manage a set of elements sorted in ascending order. The NavigableSet interface extends SortedSet and provides access to elements by matching one or more specific values.
The Queue Interface
The Queue interface extends the Collection interface to provide an implementation of a queue. In a queue, we can remove the elements only from the head.
The Queue interface provides the functionality to add, remove, access, and examine the queue elements. Typically, queues implement a First-In-First-Out(FIFO) behaviour. The Dequeue interface extends the Queue interface to support double-ended queues.
The Map Interface
The Map interface is a generic interface that provides a way to store key-value pairs. A map is an object that maps keys to values. Keys are unique which identify the appropriate values.
Some of the important methods of the Map interface:
V get (Object k):Returns the value associated with the key k.
V put(K k, V v):Stores a value v to the map with the key k.
boolean containsKey(Object k):Searches for the key k and returns true if found.
boolean containsValue(Object v):Searches for the value v and returns true if found.
The other interfaces that support maps in Java :
Map.Entry: An inner class of Map that supports key/value pairs in a map.
SortedMap: An interface that extends the Map interface. A SortedMap instance maintains keys in ascending order.
NavigableMap: An interface that extends the SortedMap interface and handles retrieval by searching for the closest match.
Happy Learning 🙂