The Java programming language has many features that help coders write programs efficiently. Java Collection Framework is one such functionality offered by JDK 1.2 that allows users to use the group of objects. Java collection framework provides many methods that enable coders to write efficient code to improve code implementation and reusability. Read this article to learn more about the Java collection framework and how to use it.
The framework can be described as code classes and interfaces available for use. A coder need not create a new framework every time a new feature needs to be implemented. Frameworks and collection of classes are available as part of optimal design in object-oriented architecture such that all classes perform similar tasks. The collection framework is one such framework in the Java programming language. A collection is a group of objects that can be represented as a single unit. The Java collection framework was introduced in JDK 1.2, and it is a set of collection classes. The two primary interfaces of the collection framework are the collection interface and the map interface. Java.util.
The image below illustrates the Java collection hierarchy as defined in java.util package. The interfaces and classes of the Java collections framework are described in the article below. The hierarchy collection example in Java has also been explained.
Source: https://u-next.com/blogs/java/collection-framework-in-java/
Before the collection framework was introduced in JDK 1.2, grouping objects in Java, such as vectors, arrays, and hashtables, performed the same tasks. But the issue with these collection objects was the lack of a standard interface. Moreover, all these collections accomplish the same task using different methods. However, it was difficult for a user to remember all the methods, constructors, and syntax. Thus, a typical Java collection framework was introduced.
The collection interface consists of the interfaces inside the collection framework implemented by all the classes present in the collection framework. The collection interface defines the methods that the collection framework can use. We know that interfaces contain the declaration of functions implemented by the classes. The collection framework does the same. The interfaces in the collection framework declare the methods. The classes in the collection framework implement these interfaces. Also, the classes define the methods to ensure the use of techniques. In other words, the collection interfaces form the foundation for the collection framework. Below are the collection interfaces:
The iterable interface is the root interface extended by the collection interface. Therefore, all the interfaces and classes inherit from the iterable interface. This interface helps provide the iterator to the collection. It has an abstract method called iterator(). The syntax is as below:
Iterator iterator();
List interface is like the child interface of the collection interface. It makes use of the list data type in which the objects are stored. List interface also allows storing duplicate data in the list. Different classes that implement the list interface include Vector, ArrayList, Stack, LinkedList. The subclasses implement the list interface, so the list object can be instantiated using subclasses.
Below is the syntax to instantiate the subclasses mentioned here:
List <Datatype> obj1 = new ArrayList();
List <Datatype> obj2 = new LinkedList();
List <Datatype> obj3 = new Stack();
List <Datatype> obj4 = new Vector();
<Datatype> is the data type of the collection object in Java obj1, obj2, obj3, and obj4.
The classes given below implement the list interface:
i) ArrayList: The class ArrayList implements the list interface and provides the feature of a dynamic array in Java. This type of array is slow compared to the usual array, but it is quite useful when extensive array manipulation is required. The ArrayList size changes as the elements are removed or added to the list. It cannot be used for primitive data types and needs wrapper class in such cases.
Below is the code snippet that illustrates the use of an ArrayList:
Import java.util.*;
Class CollectionExample1
{
Public static void main(String args[])
//Instantiate ArrayList
ArrayList<String> Subject = new ArrayList<String>();
//Adding subjects in the ArrayList
Subject.add(“English”);
Subject.add(“Mathematics”);
Subject.add(“Science”);
Subject.add(“Hindi”);
//Using iterator to traverse the ArrayList
Iterator i1 = Subject.iterator();
While(i1.hasNext())
System.out.println(i1.next());
}
Output:
English
Mathematics
Science
Hindi
ii) LinkedList The LinkedList class implements the list interface. It ensures faster manipulation as it does not involve shifting. The elements are stored internally as a doubly-linked list. It can also store duplicate elements.
Below is the code snippet that illustrates the use of LinkedList:
Class CollectionExample2
//Instantiate LinkedList
LinkedList<String> EmpName = new LinkedList<String>();
//Adding Names in the LinkedList
EmpName.add(“Peter”);
EmpName.add(“Mary”);
EmpName.add(“John”);
EmpName.add(“Rachael”);
//Using iterator to traverse the LinkedList
Iterator<String> i1 = EmpName.iterator();
Peter
Mary
John
Rachael
iii) Vector: Vector is similar to ArrayList and stores the elements in a dynamic array. It gets synchronized, unlike ArrayList, and some methods of the vector are not even part of the collection interface.
Below is the code snippet that illustrates a vector:
Class CollectionExample3
//Instantiate Vector
Vector<String> Subject = new Vector<String>();
//Adding subjects in the Vector
//Using iterator to traverse the Vector
iv) Stack: A stack is the collection class, a subclass of vector class that implements the list interface. This class contains the methods defined or implemented in the vector class. Apart from the methods of vector class, it has processes, such as boolean push(), boolean push (Object o), and boolean peek().
Below is the code snippet that illustrates stack usage:
Class CollectionExample4
//Instantiate Stack
Stack<String> Subject = new Stack<String>();
//Adding subjects in the Stack
Subject.push(“English”);
Subject.push(“Mathematics”);
Subject.push(“Science”);
Subject.push(“Hindi”);
// Remove the last element from the Stack
Subject.pop();
//Using iterator to traverse the Stack
3. Queue Interface
The queue interface is the list that stores the elements. It works like a normal queue, i.e., First In First Out (FIFO). In this interface, the order of storing the elements matter. For example, in the ticketing system, a person higher on the waiting list gets ticket confirmation first on cancellation of a reservation. The queue interface has classes, such as Deque, Priority Queue, and ArrayDeque. All these classes implement the queue interface. Below is the syntax to instantiate the queue interface:
Queue <Data Type> obj1 = new PriorityQueue();
Queue <Data Type> obj2 = new ArrayDeque();
Obj1 and obj2 are the data types of objects obj1 and obj2.
Below are some classes that implement the Queue interface:
i) Priority Queue: The queue interface is implemented by the PriorityQueue class—the class stores the objects or the elements that need to be processed according to priorities. The class does not allow the queue to store null values.
Below is the code illustrating PriorityQueue:
Class CollectionExample5
//Instantiate PriorityQueue
PriorityQueue<String> Subject = new PriorityQueue<String>();
//Adding subjects in the PriorityQueue
System.out.println(Subject.element() + “ is the head of the queue”);
System.out.println(queue.peek()+ “ is the head of the queue”);
Subject.remove();
Subject.poll();
System. Out.println(“After removing the elements, the queue has elements”)
//Using iterator
English is the head of the queue.
After removing the elements, the queue has elements.
4. Deque Interface
The queue interface is extended by the deque interface. Unlike the queue interface, elements can be added or removed from this list from both sides. This is the reason it is called the deque interface. The deque interface is also called the double-ended queue. It allows operations to be performed on both sides of the queue. Below is the syntax to instantiate the deque interface:
Dequ1 <Data Type> obj = new ArrayDeque();
i) ArrayDeque
ArrayDeque is the class that implements the deque interface. It allows the use of deque. ArrayDeque allows you to add a new element or remove the existing element from the array on both sides. ArrayDeque works faster than stack and ArrayList. It has no restriction on capacity.
Below is the code illustrating ArrayDeque
Public class CollectionExample6
//Create ArrayDeque
Deque<String> Subject = new ArrayDeque<String>);
Subject.add(“Mathematics”)’
// Traverse the elements of ArrayDeque
for(String s1 : Subject)
System.out.println(s1);
5. Set interface
The set interface is a Java interface that does not store elements in order and does not store duplicate elements. It is present in the java.util package. The collection interface is extended by the set interface. The set interface is implemented by classes, such as TreeSet, HashSet, and LinkedHashSet. Below is the syntax to instantiate the classes that implement the set interface:
Set <Data Type> obj1 = new HashSet <Data Type>();
Set <Data Type> obj2 = new LinkedHashSet <Data Type>();
Set <Data Type> obj3 = new TreeSet <Data Type>();
Here, Data Type refers to the data type of the instances obj1, obj2, and obj3.
i) HashSet
HashSet is the class that implements the set Interface. The class also represents the collection, where hash tables and hashing are used for storage. HashSet allows you to store unique elements only.
Below is the code illustrating a HashSet
Public class CollectionExample7
//Create Hashset
HashSet<String> Subject = new HashSet<String>();
// Traverse the elements of HashSet
Iterator<String> i1 = set.iterator();
ii) LinkedHashSet
LinkedHashSet is another class that implements the set interface in Java and extends the HashSet class. A LinkedHashSet class allows storing unique elements and null elements, and it also maintains the insertion order.
Below is the code illustrating a LinkedHashSet
Public class CollectionExample8
//Create LinkedHashset
LinkedHashSet<String> Subject = new LinkedHashSet<String>();
// Traverse the elements of LinkedHashSet
6. SortedSet Interface
SortedSet is an interface in Java that follows an order while storing elements. The elements are stored in increasing order in a SortedSet. Also, it is a method to store elements in ascending order, preventing them from being arranged in their natural order.
Below is the syntax to instantiate the SortedSet interface:
SortedSet <DataType> obj = new TreeSet();
Data Type is the data type of instance obj, and a TreeSet is a class that implements the SortedSet interface.
TreeSet
TreeSet is a Java collection class that implements the SortedSet interface. TreeSet class stores the elements in the tree structure. TreeSet allows only unique elements to be stored in increasing order. The TreeSet class is a quick method to access the elements stored in it.
Below is the code illustrating a TreeSet
//Create TreeSet
TreeSet<String> Subject = new TreeSet<String>();
// Traverse the elements of TreeSet
Below is the list of function/methods available in a collector framework:
Given below are the advantages of the Java collection framework:
The Java collection framework is useful for coders to write efficient code in Java. In this article, the interfaces and classes of the Java collection framework have been explained with examples. We hope the article helped you understand the concept well. You can find similar articles on Jigsaw Academy to learn more about the different concepts in Java and other technical topics. You can also opt for the Master Certificate in Full Stack Development to learn full-fledged Java.
Fill in the details to know more
What Is the Use of Wrapper Class in Java?
March 22, 2023
What Is Clean Coding in Java?
March 21, 2023
What Are the New Features of Java 17?
What Is File Handling in Java?
March 16, 2023
What Is Data and Time Function in Java?
March 11, 2023
Top 10 Emerging Technologies Blogs To Read In 2023
December 15, 2022
History of Java Programming Language| UNext
November 18, 2022
An Advanced Guide to Shell Scripting in Unix!
November 15, 2022
Java Tutorial For Beginners
September 28, 2022
What Is Data Abstraction?
August 24, 2022
Encapsulation In Oops With Example
August 23, 2022
Add your details:
By proceeding, you agree to our privacy policy and also agree to receive information from UNext through WhatsApp & other means of communication.
Upgrade your inbox with our curated newletters once every month. We appreciate your support and will make sure to keep your subscription worthwhile