What is a Collection in Java?
Java collection is a single unit of objects. Before the Collections Framework, it had been hard for programmers to write down algorithms that worked for different collections. Java came with many Collection classes and Interfaces, like Vector, Stack, Hashtable, and Array.
In JDK 1.2, Java developers introduced the Collections Framework, an essential framework to help you achieve your data operations.
Why Do We Need Them?
- Reduces programming effort & effort to study and use new APIs
- Increases program speed and quality
- Allows interoperability among unrelated APIs
- Reduces effort to design new APIs
- Fosters software reuse
Methods Present in the Collection Interface
No | Method | Description |
---|---|---|
1 | Public boolean add(E e) | To insert an object in this collection. |
2 | Public boolean remove(Object element) | To delete an element from the collection. |
3 | Default boolean removeIf(Predicate super E> filter) | For deleting all the elements of the collection that satisfy the specified predicate. |
4 | Public boolean retainAll(Collection> c) | For deleting all the elements of invoking collection except the specified collection. |
5 | Public int size() | This return the total number of elements. |
6 | Publicvoid clear() | This removes the total number of elements. |
7 | Publicboolean contains(Object element) | It is used to search an element. |
8 | PublicIterator iterator() | It returns an iterator. |
9 | PublicObject[] toArray() | It converts collection into array. |
Collection Framework Hierarchy

List Interface
This is the child interface of the collection interface. It is purely for lists of data, so we can store the ordered lists of the objects. It also allows duplicates to be stored. Many classes implement this list interface, including ArrayList, Vector, Stack, and others.
Array List
- It is a class present in java. util package.
- It uses a dynamic array for storing the element.
- It is an array that has no size limit.
- We can add or remove elements easily.
Linked List
- The LinkedList class uses a doubly LinkedList to store elements. i.e., the user can add data at the initial position as well as the last position.
- It allows Null insertion.
- If we’d wish to perform an Insertion /Deletion operation LinkedList is preferred.
- Used to implement Stacks and Queues.
Vector
- Every method is synchronized.
- The vector object is Thread safe.
- At a time, one thread can operate on the Vector object.
- Performance is low because Threads need to wait.
Stack
- It is the child class of Vector.
- It is based on LIFO (Last In First Out) i.e., the Element inserted in last will come first.
Queue
A queue interface, as its name suggests, upholds the FIFO (First In First Out) order much like a conventional queue line. All of the elements where the order of the elements matters will be stored in this interface. For instance, the tickets are always offered on a first-come, first-serve basis whenever we attempt to book one. As a result, the ticket is awarded to the requester who enters the queue first. There are many classes, including ArrayDeque, PriorityQueue, and others. Any of these subclasses can be used to create a queue object because they all implement the queue.
Dequeue
The queue data structure has only a very tiny modification in this case. The data structure deque, commonly referred to as a double-ended queue, allows us to add and delete pieces from both ends of the queue. ArrayDeque, which implements this interface. We can create a deque object using this class because it implements the Deque interface.
Set Interface
A set is an unordered collection of objects where it is impossible to hold duplicate values. When we want to keep unique objects and prevent object duplication, we utilize this collection. Numerous classes, including HashSet, TreeSet, LinkedHashSet, etc. implement this set interface. We can instantiate a set object with any of these subclasses because they all implement the set.
LinkedHashSet
- The LinkedHashSet class extends the HashSet class.
- Insertion order is preserved.
- Duplicates aren’t allowed.
- LinkedHashSet is non synchronized.
- LinkedHashSet is the same as the HashSet except the above two differences are present.
HashSet
- HashSet stores the elements by using the mechanism of Hashing.
- It contains unique elements only.
- This HashSet allows null values.
- It doesn’t maintain insertion order. It inserted elements according to their hashcode.
- It is the best approach for the search operation.
Sorted Set
The set interface and this interface are extremely similar. The only distinction is that this interface provides additional methods for maintaining the elements' order. The interface for handling data that needs to be sorted, which extends the set interface, is called the sorted set interface. TreeSet is the class that complies with this interface. This class can be used to create a SortedSet object because it implements the SortedSet interface.
TreeSet
- Java TreeSet class implements the Set interface it uses a tree structure to store elements.
- It contains Unique Elements.
- TreeSet class access and retrieval time are quick.
- It doesn’t allow null elements.
- It maintains Ascending Order.
Map Interface
It is a part of the collection framework but does not implement a collection interface. A map stores the values based on the key and value Pair. Because one key cannot have numerous mappings, this interface does not support duplicate keys. In short, The key must be unique while duplicated values are allowed. The map interface is implemented by using HashMap, LinkedHashMap, and HashTable.
HashMap
- Map Interface is implemented by HashMap.
- HashMap stores the elements using a mechanism called Hashing.
- It contains the values based on the key-value pair.
- It has a unique key.
- It can store a Null key and Multiple null values.
- Insertion order isn’t maintained and it is based on the hash code of the keys.
- HashMap is Non-Synchronized.
- How to create HashMap.
LinkedHashMap
- The basic data structure of LinkedHashMap is a combination of LinkedList and Hashtable.
- LinkedHashMap is the same as HashMap except above difference.
HashTable
- A Hashtable is an array of lists. Each list is familiar as a bucket.
- A hashtable contains values based on key-value pairs.
- It contains unique elements only.
- The hashtable class doesn’t allow a null key as well as a value otherwise it will throw NullPointerException.
- Every method is synchronized. i.e At a time one thread is allowed and the other threads are on a wait.
- Performance is poor as compared to HashMap.
This blog illustrates the interfaces and classes of the java collection framework. Which is useful for java developers while writing efficient codes. This blog is intended to help you understand the concept better.
At Sanesquare Technologies, we provide end-to-end solutions for Development Services. If you have any doubts regarding java concepts and other technical topics, feel free to contact us.
Does your Project Demand Expert Assistance?
Contact us and let our experts guide you and fulfil your aspirations for making the project successful
