The List interface is a child of Collection Interface. The List interface is found in java.util
package and inherits the Collection interface.
An ArrayList in Java is implemented as a resizable array, also known as a dynamic array. It provides an interface to work with a dynamically sized list of elements, allowing for efficient insertion, deletion, and random access.
A vector provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.
Let’s see how to create a List object using Vector class:
Stack is a class that is implemented in the collection framework and extends the vector class models and implements the Stack data structure. The class is based on the basic principle of last-in-first-out.
Let’s see how to create a List object using Stack class:
LinkedList is a class that is implemented in the collection framework which inherently implements the linked list data structure.
It is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part.
Let’s see how to create a List object using LinkedList class:
The Set interface extends the Collection interface. It represents the unordered set of elements which doesn’t allow us to store the duplicate items.
HashSet is one of the widely used classes which implements the Set interface.
LinkedHashSet class which is implemented in the collections framework is an ordered version of HashSet that maintains a doubly-linked List across all elements.
The SortedSet interface present in java.util package extends the Set interface present in the collection framework. It is an interface that implements the mathematical set.
TreeSet class which is implemented in the collections framework and implementation of the SortedSet Interface and SortedSet extends Set Interface.
Let’s see how to create a Set object using TreeSet class:
In Java, Map Interface is present in java.util package represents a mapping between a key and a value. Java Map interface is not a subtype of the Collection interface. Therefore it behaves a bit differently from the rest of the collection types.
HashMap provides the basic implementation of the Map interface of Java. It stores the data in (Key, Value) pairs.
To access a value one must know its key.
LinkedHashMap is just like HashMap with the additional feature of maintaining an order of elements inserted into it.
Let’s see how to create a Map object using LinkedHashMap class:
The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. This proves to be an efficient way of sorting and storing the key-value pairs.
Let’s see how to create a Map object using TreeMap class:
The Queue interface is present in java.util package and extends the Collection interface is used to hold the elements about to be processed in FIFO(First In First Out) order.
Deque interface present in java.util package is a subtype of the queue interface. The Deque is related to the double-ended queue that supports the addition or removal of elements from either end of the data structure. It can either be used as a queue(first-in-first-out/FIFO) or as a stack(last-in-first-out/LIFO). Deque is the acronym for double-ended queue.
The Comparable interface in Java is essential for defining a natural ordering for a class. By implementing Comparable, a class provides a standardized way to compare instances of that class. This natural ordering is particularly useful for sorting elements in collections like TreeSet or when utilizing sorting algorithms such as Collections.sort().
The Comparable interface contains a single method:
Here, T represents the type of objects being compared. The compareTo
method returns a negative integer, zero, or a positive integer based on whether the current object is less than, equal to, or greater than the object being compared.
For example, consider a Person class that implements the Comparable interface to define a natural ordering based on age:
In Java, the Comparator interface provides a means to define custom ordering for objects in collections. It is particularly useful when sorting objects in a way that deviates from their natural order or when working with classes that don't implement Comparable.
Here's a simple example of how you might use a Comparator to sort a list of Person objects based on their ages: