Popular Branches
MBA
B.Tech
BBA
BSc
Updated on 03rd July, 2024 , 6 min read
The collection in Java is a framework that gives an architecture that stores and manipulates the group of objects. Java collection means a single unit of object. Java collection can archive all the operations that you perform on data such as searching, sorting, insertion, manipulation or delectation. Java framework provides many interfaces (set, queue, list, deque) and classes ( ArrayList, Vector, Likedlist, PriorityQueue, HashSet, LikedHastSet, Treeset)
In simpler terms, we can define it as:
Any group of individual objects that can be represented as a single unit is called a Java collection of objects. In Java, a separate framework named the “Collection framework” has been defined in JDK2.1 which holds all the Java collection classes.
The names and details of the Java collection are given below:
Names | Details |
List Interface | Abstract List Class Abstract Sequential List Class Array List Vector Class Stack Class LikedList Class |
SortedSet Interface | NavigableSet Interface TreeSet ConcurrentSkipListSet Class |
Map Interface | Sorted Map Interface NavigableMap Interface ConcurrentMap Interface HashMap Class IdentityHashMap Class HashTable Class Properties Class |
Set Interface | Abstract Set Class EnumSet Class ConcurrentHashMap Class LikedHashSet Class HashSet Class |
Deque Interface | BlockingDeque Interface ConcurrentLiked Deque Class ArrayDeque Class |
Queue Interface | Blocking Queue Interface Abstract Queue Class Priority Queue Class Priority Blocking Queue Class Delay Queue Class LikedBlocking Queue Class Liked Transfer Queue |
Also see: Abstract Class in Java
A framework in java is a set of classes and interfaces.It provides a ready-made architecture which helps in implementing a new feature or a class, However, an optimal object-oriented design always includes a framework with a collection of classes such that all classes perform the same kind of task.
The collection interface is the interface which is implemented by the collection framework. It is a declaration of the methods that every collection should have. In simpler words, we can say that the collection interface builds the foundation on which the collection framework depends.
The following interfaces are present in the framework:
There are many methods proclaimed in the collection interface, they are as follows:
Method | Description |
add(Object). | In this method it is used to add an object to the collection. |
addAll(Collection c) | It is used to add all the elements in the given collection to this collection. |
clear() | This method helps in removing all of the elements from this collection. |
contains(Object o) | This method helps in returning true if the collection contains the specified element. |
containsAll(Collection c) | This method keeps returns true if the collection holds all of the elements in the given collection. |
equals(Object o) | In this method it compares the specified object with this collection for equality. |
hashCode() | This process is used to return the hash code value for this collection. |
isEmpty() | It returns true if this collection contains no elements. |
iterator() | It returns an iterator over the elements in this collection. |
parallel stream() | This method returns a parallel Stream with this collection as its source. |
remove(Object o) | It removes the given object from the collection and if there are any copied values, then this method erases the first occurrence of the object. |
The ArrayList class implements the List interface. It uses a dynamic array to store the duplicate elements of different data types. The ArrayList class keeps up the insertion order and is uncoordinated. The elements stored in the ArrayList class can be randomly accessed. The diameter of an ArrayList is increased by itself if the collection grows or reduces if the objects are removed from the collection.
LinkedList implements the Collection interface. It uses a doubly linked list internally to store the elements. It can store the duplicate elements. It continues the insertion order and it is uncoordinated. The elements are linked using pointers and addresses. Each element is known as a node.
A vector provides dynamic arrays in Java. Though it may be slower than standard arrays it can be helpful in programs where lots of manipulation in the array is needed. Vector uses a dynamic array for various purposes like storing the data elements. It is similar to ArrayList. However, It is synchronized and contains many methods that are not part of the Collection framework.
The stack is the subclass of Vector. It implements the last-in-first-out data structure, i.e., Stack. The class is based on the basic principle of LIFO which stands for last-in-first-out. In addition to the basic push and pop operations, the class gives us the three main functions:
1 empty
2 search
and 3 peeks.
These classes can be viewed as the subclass of the Vector.
The queue interface maintains the first-in-first-out order. It can be defined as an ordered list used to hold the elements about to be processed. This interface is dedicated to storing all the elements where the order of the elements matters. Therefore, the person whose request arrives first in the queue gets the ticket.
The Deque interface extends the Queue interface. In Deque, we can erase and add the elements from both sides. Deque stands for the double-ended queue which lets us perform the operations from both ends. This interface extends the queue interface. The class which implements this interface is ArrayDeque. The ArrayDeque class implements the Deque interface; it can instantiate a deque object with this class.
The ArrayDeque class which is implemented in the collection framework provides us with a way to apply a resizable array. This kind of array grows and allows the users to add or erase an element from both sides of the queue. The ArrayDeque class executes the Deque interface. It facilitates the use of the Deque. Unlike queue, we can add or delete the elements from both ends.ArrayDeque is faster than ArrayList and Stack and has no capacity restrictions.
A set is an unorganized collection of objects whose copied values cannot be stored. This collection is used when you wish to avoid the duplication of the objects and wants to store only the unique objects. The set interface is applicable on various classes like HashSet, LinkedHashSet, TreeSet,etc. Set Interface in Java is present in Java. util package. It extends the Collection interface. It represents the unorganized set of elements which don't allow you to store the copied items.
The SortedSet interface is very likewise to the set interface. The major difference that can be viewed is the interface has extra methods which helps in maintaining the order of the elements. This interface enlarges the set interface and it can be used to handle the data that needs to be sorted. The elements of this interface are arranged in the ascending (increasing) order. The SortedSet provides extra methods that enable the natural ordering of the required elements.
A map is a data structure which supports the key-value pair for the mapping of data. A map is useful if there is data and we wish to perform operations on the basis of the key.This interface doesn’t support duplicate keys because the same key cannot do multiple mappings, but it allows to copy values in different keys.
The advantages of a collections framework are as follows:
Also Read: Difference between Python and Java
Pilot Salary in India 2024: Starting Salary, Requirements, Qualifications, Per Month Salary
By - Nikita Parmar 2024-09-06 10:59:22 , 6 min readThe collection in Java is a framework that gives an architecture that stores and manipulates the group of objects. Java collection means a single unit of object. Java collection can archive all the operations that you perform on data such as searching, sorting, insertion, manipulation or delectation.
A framework in java is a set of classes and interfaces.It provides a ready-made architecture which helps in implementing a new feature or a class, However, an optimal object-oriented design always includes a framework with a collection of classes such that all classes perform the same kind of task.
The collection interface is the interface which is implemented by the collection framework. It is a declaration of the methods that every collection should have. In simpler words, we can say that the collection interface builds the foundation on which the collection framework depends.
The ArrayList class implements the List interface. It uses a dynamic array to store the duplicate elements of different data types. The ArrayList class keeps up the insertion order and is uncoordinated. The elements stored in the ArrayList class can be randomly accessed.
It helps in reducing programming efforts and it also provides data structures and algorithms so one doesn't have to write them yourself. It helps in increasing performance and it provides high-performance implementations on the data structures and algorithms.