In this article, we will be looking at the Collection in JAVA which is very useful for practicing DSA as it contains all types of data structures like Stacks, Queues, LinkedList, etc.
The JAVA Collection is a framework that provides different data structures which can be operated upon. We can perform different operations like sorting, searching, insertion, deletion, and manipulation of the data structure and the data within.
Before understanding and knowing more about the collection framework let us try to understand the terms framework, interface, and class.
Framework – A Framework is a pre-written code that can be used to work on directly instead of writing code from scratch every time. For example, if we take the JAVA collection framework, we can use the data structures directly instead of writing the code from scratch.
Interface – An interface is a blueprint of a class; it consists of static constants and abstract methods. An interface is implemented by a class and the abstract methods are defined inside that class.
Class – A class is a template that can be used to create objects with properties and methods. One class can extend another class i.e., inherit the properties and methods of a class.
What is Collection Interface?
The JAVA collection interface has several classes using which are used to represent/store groups of objects under a single unit, several operations like sorting, searching, insertion, deletion, and manipulation of data can be done on these units.
Hierarchy of Collection Interface
All the package that contains all the classes and interfaces for the collections framework is java.util
Now let us look at each available interface and the classes implemented on those interfaces.
List Interface
The list interface is used to implement different ordered collection objects. These objects may also contain duplicate values.
The list interface is implemented by the following classes:
- ArrayList
- LinkedList
- Vector
- Stack
Let us look at each of the classes that are implementing the list interface.
ArrayList
The ArrayList class implements the List Interface. The ArrayList is unlike any array in JAVA it is dynamic. We need not worry about the size of the array while inserting the elements into the array. An ArrayList also stores duplicate values of different types. Random access to elements is possible in an ArrayList using the index values.
Here is an example program to understand ArrayList and its methods
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo{
public static void main(String[] args){
// List<Integer> n = new LinkedList<>();
List<Integer> n = new ArrayList<>();
n.add(10);
n.add(20);// adding an element at the end of the ArrayList
System.out.println(n);
n.add(1,30);// adding an element at given index
System.out.println(n);
n.set(1,15);// to set a value in the ArrayList
System.out.println(n);
System.out.println(n.contains(30));// checks if a given element is present in the list
n.remove(Integer.valueOf(20));// removes the given type of value from the ArrayList
System.out.println(n);
n.remove(1);// removes an element at the given index value
System.out.println(n);
n.clear();// clears the whole list by removing all the elements in the ArrayList
System.out.println(n);
//Traversing an ArrayList
// using for loop
for(int i=0;i<n.size();i++){
System.out.println(n.get(i));
}
// using a for each loop
for (Integer integer : n) {
System.out.println(integer);
}
// using iterator class used to iterator through the collection
Iterator<Integer> itr = n.iterator();
while(itr.hasNext())
System.out.println(itr.next());
}
}
Output:
[10, 20]
[10, 30, 20]
[10, 15, 20]
false
[10, 15]
[10]
[]
LinkedList
The LinkedList class in a List interface is used for implementing a doubly linked list to store elements. The order of the elements is maintained while inserting them in a LinkedList. The manipulation of data i.e., adding or removing data is much faster as no shifting is involved.
Here is an example program to understand LinkedList and its methods
import java.util.*;
public class TestJavaCollection2{
public static void main(String args[]){
LinkedList<String> al=new LinkedList<String>();
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator<String> itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Output:
Ravi
Vijay
Ravi
Ajay
In the next article, we are going to have a look at the other topics in collection framework of java.