Updated March 29, 2023
Introduction to java collection api
Java collection api is mainly defined as the set of classes and interfaces that can be supported with the user operations on the specified collection of objects and these classes and interfaces are more flexible, more powerful and more regular than that of the other features which are having the classes and interfaces like vector, map, arrays, list and even other util classes and functions will be used to replaces for the datas and details of their representation which has the advantages of the collection framework and it provides the interoperability between the unrelated API it supports the language.
Java Collection API and Implementations
The collection API is the Application Programming Interface and it provided the reusable functionality for a core set of the algorithms that can be operated with the list collections. It has some util packages and it contains some default classes and interfaces that can be required by the collection framework. The following types are some java collections APIs are,
- Collection Interface: This is the parent root of the collection hierarchy and it implements the methods for creating the application.
- Iterator Interface: Using this interface we can iterate the datas for the loops like “for, while, etc”.
- Set Interface: It is one of the default interfaces and it does not support the duplicate elements in the list.
- List Interface: The list interface is used to store and access the collection datas in the memory. It has some default methods for performing the operations.
- Queue Interface: This interface follows the FIFO(First-In-First-Out) order by the data structure which means that the element which is inserted first will be removed from the first, if we want to add then it will add the new element in the list.
- Dequeue Interface: This deque knew as a double-ended queue and it is the ordered collection of data items which is similar to the queue. Like that it has two ends like front and rear position.
- Map Interface: It is mostly used interface in the collection and using the default methods the values are operated by using the key-value pair items.
- List-Iterator Interface: The list iterator interface allows for traversing the elements in any of the direction and also it obtains the current position in the list.
- Sorted-Set Interface: It is the set interface that will be maintained in the ascending order of the elements. This will be generally used in the ordered sets such as the word lists and other membership areas.
- Sorted Map Interface: This is also the same as Sorted-Set but here the datas ordered will be the key-value pairs.
Examples of java collection api
Here are the following examples mentioned below.
Example #1
Code:
import java.util.*;
public class Sample {
public static void main(String[] args) {
ArrayList<String> aLst=new ArrayList<String>();
aLst.add("Your first array list value is January");
aLst.add("Your second array list value is February");
aLst.add("Your third array list value is March");
aLst.add("Your four array list value is April");
aLst.add("Your fifth array list value is May");
Iterator itr=aLst.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
collec collec = new HashSet();
collec.add("Your sixth array list value is June");
collec.add("Your seventh array list value is July");
collec.add("Your eigth array list value is August");
collec.add("Your ninth array list value is September");
collec.add("Your tenth array list value is October");
collec.add("Your eleventh array list value is November");
collec.add("Your twelth array list value is December");
for(obj obj : collec) {
System.out.println(obj);
}
LinkedList<String> linklst=new LinkedList<String>();
linklst.add("Welcome To The RK Group of Constructions");
linklst.add("Welcome To The KR Group of Constructions ");
linklst.add("Welcome To The SV Group of Constructions,BSCPL Infrastructure");
linklst.add("Welcome To The VS Group of Constructions, Ozone Group Bangalore");
linklst.add("Welcome To The VSR Group of Constructions, BSCPL Infrastructure");
linklst.add("Welcome To The Royal Knit Group of Constructions, Shriram Properties");
linklst.add("Welcome To The Mithun Constructions, Pacifica Companies");
linklst.add("Welcome To The JK Group of Constructions, DRA Group");
Iterator<String> itr1=linklst.iterator();
while(itr1.hasNext()){
System.out.println(itr1.next());
}
Vector<String> v=new Vector<String>();
v.add("Welcome To The Vijay Shanthi Builders, Prestige Group,Olymia Group, Godrej Properties,Brigade Group");
v.add("Welcome To The Kay Arr and Co Rio Grande, Tata value Homes, Brigade Group, Akshaya Homes");
v.add("Welcome To The JB, Eternia, ITC Grand Chola, DEEJOS Architects, Bharath Engineering Construction company,Jehovah Nissi Archfirm");
Iterator<String> itr2=v.iterator();
while(itr2.hasNext()){
System.out.println(itr2.next());
}
}
}
Output:
The above example we used some collection list classes, interfaces and methods used for to implement the java application based on the various scenarios.
Example #2
Code:
import java.util.*;
public class Sample{
public static void main(String[] args) {
Deque<String> dqueue = new ArrayDeque<String>();
dqueue.add("Laptop");
dqueue.add("Personal Computer");
dqueue.add("Tablet");
for (String str : dqueue) {
System.out.println(str);
}
}
}
Output:
In the above example, we used the Deque collection list interface and we created the object of the ArrayDeque<String> to add, store and retrieve datas. Using for loop to iterate the values in the dqueue interface and it will be stored in the String datatype.
Java Collection AP- features and benefits
The java collection API is the application programming interface for implementing the application in the various operations. The collections in java are the framework is the util type and it is implemented with the application for the database storage areas and the data structure areas to perform the CRUD operations. Mainly the collections API is provided with the support for the objects with the specified orders in the many ways and also the API is the reusable functionality with the core set of the algorithms that can be operated on the specified collection lists. The collection API is the framework that provides an architecture to store and manipulate the data for the group of projects and basically, it is the package of the data structures that will be included in the Array lists, Linked lists, Hash sets, Hash map etc. The main benefit is to reduce the effort that can be required to design and implement the task.
Conclusion
The java language is a high-level language and that can be used to implement both standalone and web-based applications. It depends on the required API that will be used in the code and they will call it on the different areas like that collection API to perform the user operations regarding storing and manipulate the datas.
Recommended Articles
This is a guide to java collection API. Here we discuss the java collection API along with the Examples, features and benefits. You may also have a look at the following articles to learn more –