Collection in Java2

Collection in Java2

Vector

Vector uses dynamic array-type objects which are similar to the ArrayList. A vector is synchronized i.e., only one thread at a time can access them which differentiates them from the ArrayList which is not synchronized as multiple threads can access them at a time.

Let’s look at an example program to understand vectors

import java.util.*;  
public class TestJavaCollection3{  
public static void main(String args[]){  
Vector<String> v=new Vector<String>();  
v.add("Ayush");  
v.add("Amit");  
v.add("Ashish");  
v.add("Garima");  
Iterator<String> itr=v.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
}  
}  
} 
Output:
Ayush
Amit
Ashish
Garima

Stack

The stack object implements the Last-in-First-out method of inserting and removing elements from the data structure. A stack is very similar to a Vector. Several methods define the properties of a vector-like push(), pop(), and peek().

Let’s look at an example program to implement a stack

import java.util.*;  
public class TestJavaCollection4{  
public static void main(String args[]){  
Stack<String> stack = new Stack<String>();  
stack.push("Ayush");  
stack.push("Garvit");  
stack.push("Amit");  
stack.push("Ashish");  
stack.push("Garima");  
stack.pop();  
Iterator<String> itr=stack.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
}  
}  
}  
Output:
Ayush
Garvit
Amit
Ashish

Queue Interface

he queue interface is used to implement the first-in-first-out order of insertion and deletion into a data structure. Various classes like PriorityQueue, Deque, and ArrayDeque implement the Queue interface.

Let us now look at the different classes that implement the queue interface.

  • PriorityQueue
  • Deque Interface
  • ArrayDeque

PriorityQueue

The priority queue processes the elements based on their priority. A priority queue does not allow null values to be stored in the queue.

Let’s understand this with an example,

import java.util.*;  
public class TestJavaCollection5{  
public static void main(String args[]){  
PriorityQueue<String> queue=new PriorityQueue<String>();  
queue.add("Amit Sharma");  
queue.add("Vijay Raj");  
queue.add("JaiShankar");  
queue.add("Raj");  
System.out.println("head:"+queue.element());  
System.out.println("head:"+queue.peek());  
System.out.println("iterating the queue elements:");  
Iterator itr=queue.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
}  
queue.remove();  
queue.poll();  
System.out.println("after removing two elements:");  
Iterator<String> itr2=queue.iterator();  
while(itr2.hasNext()){  
System.out.println(itr2.next());  
}  
}  
}
Output:
head:Amit Sharma
head:Amit Sharma
iterating the queue elements:
Amit Sharma
Raj
JaiShankar
Vijay Raj
after removing two elements:
Raj
Vijay Raj

The ArrayDeque class implements the Deque interface. The Deque helps us insert and delete elements from both ends, unlike a normal queue. Also, the ArrayDeque is faster than the ArrayList and the Stack.

See also  9 Steps Why Rust is emerging as Developer's favorite programming language

Here is an example to understand the ArrayDeque class,

import java.util.*;  
public class TestJavaCollection6{  
public static void main(String[] args) {  
//Creating Deque and adding elements  
Deque<String> deque = new ArrayDeque<String>();  
deque.add("Gautam");  
deque.add("Karan");  
deque.add("Ajay");  
//Traversing elements  
for (String str : deque) {  
System.out.println(str);  
}  
}  
}  
Output:
Gautam
Karan
Ajay

Set Interface

A set interface in JAVA stores data in an unordered fashion and also doesn’t allow any duplicate values to be stored. We only store at most one null value in a set.

The classes that implement the Set interface are:

  • HashSet
  • LinkedHashSet

HashSet

HashSet represents the collection that used a hash table for storage. Hashing is used to store the elements in the HashSet.

Let’s understand this with an example,

import java.util.*;  
public class TestJavaCollection7{  
public static void main(String args[]){  
//Creating HashSet and adding elements  
HashSet<String> set=new HashSet<String>();  
set.add("Ravi");  
set.add("Vijay");  
set.add("Ravi");  
set.add("Ajay");  
//Traversing elements  
Iterator<String> itr=set.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
}  
}  
}  
Output:
Vijay
Ravi
Ajay

LinkedHashSet

A LinkedHaset extends the HashSet and implements the Set interface. It provides a Linked Implementation of the Set interface.  It maintains the order of elements in which they were inserted.

Let’s understand this with an example,

import java.util.*;  
public class TestJavaCollection8{  
public static void main(String args[]){  
LinkedHashSet<String> set=new LinkedHashSet<String>();  
set.add("Ravi");  
set.add("Vijay");  
set.add("Ravi");  
set.add("Ajay");  
Iterator<String> itr=set.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
}  
}  
}  
Output:
Ravi
Vijay
Ajay

SortedSet Interface

The SortedSet interface provides an ordering of elements when they are inserted or deleted. The elements in the SortedSet are arranged in ascending order.

The one class that implements this interface is the TreeSet class.

TreeSet

Java TreeSet class implements the Set interface that uses a tree for storage. Like HashSet, TreeSet also contains unique elements. However, the access and retrieval time of TreeSet is quite fast. The elements in TreeSet are stored in ascending order.

See also  POWER  OF A NUMBER USING RECURSION and nCr USING RECURSION 

Look at the example below to understand the working of TreeSet,

import java.util.*;  
public class TestJavaCollection9{  
public static void main(String args[]){  
//Creating and adding elements  
TreeSet<String> set=new TreeSet<String>();  
set.add("Ravi");  
set.add("Vijay");  
set.add("Ravi");  
set.add("Ajay");  
//traversing elements  
Iterator<String> itr=set.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
}  
}  
}
Output:
Ajay
Ravi
Vijay

That’s it for this article. In the next article, we are going to take a look at STL’s in C plus plus.

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.