top
upGrad KnowledgeHut SkillFest Sale!

Search

Java Tutorial

The Java Collection is an architectural framework for storing and manipulating the group of objects. Java Collections can conduct all the activities you undertake on data such as search, sort, insert, manipulate, and delete. Java Collection refers to a single object unit. Java Collection Framework offers numerous interfaces (List, Set, Queue, Deque) and classes (TreeSet, Vector, LinkedList, PriorityQueue, ArrayList, HashSet). Advantages of Collection Framework in java: Increases program speed and quality: Increases performance by offering helpful data structures and algorithms with high-performance applications. Consistent API: The API has a number of fundamental interfaces such as Collection, Set, List, or Map. All classes that execute these interfaces (ArrayList, LinkedList, Vector, etc.) have some prevalent set of methods. Reduces programming effort:  A java programmer doesn’t have to worry about the design of Collection framework, and he can concentrate on its best use in his program. The java.util package includes all the Collection Framework classes and interfaces. Collection: It is a root interface having basic methods for example add(), remove(), contains(), isEmpty(), addAll() etc. List: Itcan contain duplicate elements and elements in the list that are ordered. Example: LinkedList and ArrayList. Set: Doesn't allow duplicate elements. Examples of Set interface implementations are HashSet (based on hashing) and TreeSet (based on balanced BST). Note that SortedSet is being implemented by TreeSet. Queue: Order elements typically in FIFO order except for exceptions such as PriorityQueue. Deque: It is possible to insert and remove elements at both ends. Allows LIFO as well as FIFO. Map: Contains pairs of key value. Does not permit duplicates. HashMap and TreeMap are examples of its implementation. SortedMap is implemented by TreeMap. The distinction between the Set and Map interface is that we only have keys in Set, whereas key, value pairs are available in Map. Iterator interface in java Iterator interface only offers the ability to iterate the collection elements forward. Methods of Iterator interface The Iterator interface has only 3 methods. MethodDescriptionpublic Object next()It returns the current element and moves the cursor pointer to the next element in collection object.public boolean hasNext()It returns true if the iterator has next element else it is false.public void remove()It removes the iterator's last returned element.Iterable Interface The Iterable interface for all collection classes is the root interface. The interface of collection extends the Iterable interface and thus all subclasses of the interface of collection also implement the Iterable interface. It includes one abstract method only.  Iterator<T> iterator() It returns the iterator over Type T elements. List Interface in java List interface is the Collection interface child. It inhibits a data structure of the list type in which we can store ordered object collection. It can have values duplicated. The ArrayList, LinkedList, Vector, and Stack classes are implementing the List interface. To instantiate the List interface, we must use: List <data-type> listType1= new ArrayList<data-type> ();   List <data-type> listType2 = new LinkedList<data-type>();   List <data-type> listType3 = new Vector<data-type> ();   List <data-type> listType4 = new Stack<data-type>(); In the List interface, there are different methods that can be used to insert, delete and access the elements from the list. Below are the classes that implement the interface of the List. ArrayList The List interface is implemented by the ArrayList class. It utilizes a dynamic array to store various kinds of information duplicate elements. The class ArrayList retains the order of insertion and is not synchronized. It is possible to access randomly the elements stored in the ArrayList class.  Take the instance below. import java.util.*;   class TestJavaArrayList{   public static void main(String args[]){   ArrayList<String> myList=new ArrayList<String>();  myList.add("Ashish");  myList.add("Rahul");   myList.add("Shivam");   myList.add("Montu");   Iterator itr= myList.iterator();   while(itr.hasNext()){   System.out.println(itr.next());   }   }   } Output of above program: Ashish  Rahul  Shivam  Montu LinkedList The Collection interface is implemented by LinkedList. It internally utilizes a dual-linked list to store the elements. The duplicate elements can be stored. It keeps the order of insertion and is not synchronized. The manipulation is quick in LinkedList because no shifting is necessary. Take the instance below.  import java.util.*;   publicclass TestJavaLinkedList{   public static void main(String args[]){   LinkedList<String> linkedList =new LinkedList<String>();   linkedList.add("Ashish");   linkedList.add("Rahul");   linkedList.add("Shivam");   linkedList.add("Montu");   Iterator<String> itr= linkedList.iterator();   while(itr.hasNext()){   System.out.println(itr.next());   }   } }  Output: Ashish  Rahul  Shivam  Montu Vector To store the information elements, the Vector utilizes a dynamic array. It's ArrayList-like. It is synchronized, however, and includes many methods that are not part of the framework for collection. Take the instance below.  import java.util.*;   publicclass TestJavaCollection3{   public static void main(String args[]){   Vector<String> myVector=new Vector<String>();   myVector.add("Sneha");   myVector.add("Priya");   myVector.add("Sonia");   myVector.add("Preeti");   Iterator<String> itr= myVector.iterator();   while(itr.hasNext()){   System.out.println(itr.next());   }   }   } Output: Sneha  Priya  Sonia  Preeti Stack The stack is Vector's subclass. It uses the data structure of the last-in-first-out, i.e. Stack. The stack includes all Vector class methods and also offers its techniques such as boolean push), (boolean peek), (boolean push(object o) defining its characteristics. Take the instance below.  import java.util.*;   publicclass TestJavaCollection4{   public static void main(String args[]){   Stack<String> myStack = new Stack<String>();   myStack.push("Ashish");   myStack.push("Priya");   myStack.push("Mohan");   myStack.push("Sneha");   myStack.push("Kangana");   myStack.pop();   Iterator<String> itr= myStack.iterator();   while(itr.hasNext()){   System.out.println(itr.next());   }   }   } Output: Ashish  Priya  Mohan  Sneha Queue Interface in java Queue interface keeps the first-in-first-out order. It can be described as an ordered list used to keep the processed elements. There are different classes that implement the Queue interface, such as PriorityQueue, Deque, and ArrayDeque. Take the instance below. Queue<Integer> queue1 = new PriorityQueue<Integer>();   Queue<Integer> queue2 = new ArrayDeque<Integer>(); PriorityQueue The Queue interface is implemented by the PriorityQueue class. It contains the components or items to be processed according to their priorities. PriorityQueue does not permit the storage of null values in the queue. Take the instance below.  import java.util.*;   publicclass TestJavaQueue{   public static void main(String args[]){   PriorityQueue<String> myQueue=new PriorityQueue<String>();   myQueue.add("Ashish ");   myQueue.add("Vijay Raj");   myQueue.add("JaiShankar");   myQueue.add("Raj");   System.out.println("Head element:"+ myQueue.element());   System.out.println("Head element:"+ myQueue.peek());   System.out.println("iterating the queue elements:");   Iterator itr= myQueue.iterator();   while(itr.hasNext()){   System.out.println(itr.next());   }   myQueue.remove();   myQueue.poll();   System.out.println("after removing 2 elements:");   Iterator<String> itr2=queue.iterator();   while(itr2.hasNext()){   System.out.println(itr2.next());   }   }   } Output: Head element:Ashish  Head element:Ashish  iterating the queue elements:  Ashish  Raj  JaiShankar  Vijay Raj  after removing 2 elements:  JaiShankar  Vijay Raj Deque Interface Deque interface expands the interface to the queue. We can delete and add the components from both sides in Deque. Deque is a double-ended queue that allows us to execute both ends of the activities. Take the instance below. Deque deque = new ArrayDeque(); ArrayDeque The Deque interface is implemented by the ArrayDeque class. It makes it easier for us to use the Deque. Unlike queue, from both ends we can add or delete the components. ArrayDeque is quicker than ArrayList and Stack and has no limitations on capability.Take the instance below.  import java.util.*;   publicclass TestJavaQueue{   public static void main(String[] args) {   Deque<String> myDeque = new ArrayDeque<String>();   myDeque.add("Priya");   myDeque.add("Sonia");   myDeque.add("Rahul");   for (String str : myDeque) {   System.out.println(str);   }   }   } Output: Priya  Sonia  Rahul Set Interface in java The Java Set Interface package includes java.util. It expands the interface for the collection. It represents the unordered set of elements that do not allow the duplicate items to be stored. We can store a null value in Set as much as possible. HashSet, LinkedHashSet, and TreeSet implements the set. Take the instance below. Set<data-type> set1 = new HashSet<data-type>();   Set<data-type> set2 = new LinkedHashSet<data-type>();   Set<data-type> set3 = new TreeSet<data-type>(); HashSet Class HashSet implements Interface Set. It is the set that utilizes a storage hash table. Hashing is used to store HashSet components. It includes distinctive elements. Take the instance below.  import java.util.*;   publicclass TestJavaSet{   public static void main(String args[]){   HashSet<String> mySet=new HashSet<String>();   mySet.add("Ashish");   mySet.add("Priya");   mySet.add("Hemu");   Iterator<String> itr= Myset.iterator();   while(itr.hasNext()){   System.out.println(itr.next());   }   }   } Output: Ashish  Priya  Hemu LinkedHashSet LinkedHashSet class reflects Set Interface's LinkedList application. It expands the class of HashSet and implements the interface set. It also includes distinctive components such as HashSet. It keeps the order of insertion and allows null components. Take the instance below.  import java.util.*;   publicclass TestJavaSet{   public static void main(String args[]){   LinkedHashSet<String> Myset=new LinkedHashSet<String>();   Myset.add("Sonia");   Myset.add("Kangana");   Myset.add("Hemu");   Iterator<String> itr= Myset.iterator();   while(itr.hasNext()){   System.out.println(itr.next());   }   }   } Output: Sonia  Kangana  Hemu SortedSet Interface SortedSet is the alternate of a Set interface that provides a total ordering on its elements. The elements of the SortedSet are arranged in the increasing (ascending) order. The SortedSet provides additional methods that inhibit the natural ordering of the elements. Take the instance below. SortedSet<data-type> set = new TreeSet data-type>();  TreeSet SortedSet is the Set interface alternative that offers a complete ordering of its components. The SortedSet components are arranged in the rising (ascending) order. The SortedSet offers extra methods that inhibit the elements ' natural order. Take the instance below.  import java.util.*;   publicclass TestJavaSet{   public static void main(String args[]){   TreeSet<String> mySet=new TreeSet<String>();   mySet.add("Raju");   mySet.add("Vijay");   mySet.add("Ashish");   Iterator<String> itr= mySet.iterator();   while(itr.hasNext()){   System.out.println(itr.next());   }   }   } Output: Ashish  Raju  Vijay
logo

Java Tutorial

Java Collection

The Java Collection is an architectural framework for storing and manipulating the group of objects. 

  • Java Collections can conduct all the activities you undertake on data such as search, sort, insert, manipulate, and delete. 
  • Java Collection refers to a single object unit. Java Collection Framework offers numerous interfaces (List, Set, Queue, Deque) and classes (TreeSet, Vector, LinkedList, PriorityQueue, ArrayList, HashSet). 

Advantages of Collection Framework in java: 

  • Increases program speed and quality: Increases performance by offering helpful data structures and algorithms with high-performance applications. 
  • Consistent APIThe API has a number of fundamental interfaces such as Collection, Set, List, or Map. All classes that execute these interfaces (ArrayList, LinkedList, Vector, etc.) have some prevalent set of methods. 
  • Reduces programming effort:  A java programmer doesn’t have to worry about the design of Collection framework, and he can concentrate on its best use in his program. 

The java.util package includes all the Collection Framework classes and interfaces. 

  • Collection: It is a root interface having basic methods for example add(), remove(), contains(), isEmpty(), addAll() etc. 
  • List: Itcan contain duplicate elements and elements in the list that are ordered. Example: LinkedList and ArrayList. 
  • Set: Doesn't allow duplicate elements. Examples of Set interface implementations are HashSet (based on hashing) and TreeSet (based on balanced BST). Note that SortedSet is being implemented by TreeSet. 
  • QueueOrder elements typically in FIFO order except for exceptions such as PriorityQueue. 
  • DequeIt is possible to insert and remove elements at both ends. Allows LIFO as well as FIFO. 
  • MapContains pairs of key value. Does not permit duplicates. HashMap and TreeMap are examples of its implementation. SortedMap is implemented by TreeMap. 

The distinction between the Set and Map interface is that we only have keys in Set, whereas key, value pairs are available in Map. 

Framework classes and interfaces

Iterator interface in java 

Iterator interface only offers the ability to iterate the collection elements forward. 

Methods of Iterator interface 

The Iterator interface has only 3 methods. 

MethodDescription
public Object next()It returns the current element and moves the cursor pointer to the next element in collection object.
public boolean hasNext()It returns true if the iterator has next element else it is false.
public void remove()It removes the iterator's last returned element.

Iterable Interface 

The Iterable interface for all collection classes is the root interface. The interface of collection extends the Iterable interface and thus all subclasses of the interface of collection also implement the Iterable interface. 

  • It includes one abstract method only.  Iterator<T> iterator() 
  • It returns the iterator over Type T elements.

List Interface in java 

List interface is the Collection interface child. It inhibits a data structure of the list type in which we can store ordered object collection. It can have values duplicated. 

The ArrayList, LinkedList, Vector, and Stack classes are implementing the List interface. 

To instantiate the List interface, we must use: 

List <data-type> listType1= new ArrayList<data-type> ();   
List <data-type> listType2 = new LinkedList<data-type>();   
List <data-type> listType3 = new Vector<data-type> ();   
List <data-type> listType4 = new Stack<data-type>();   

In the List interface, there are different methods that can be used to insert, delete and access the elements from the list. 

Below are the classes that implement the interface of the List. 

ArrayList 

The List interface is implemented by the ArrayList class. It utilizes a dynamic array to store various kinds of information duplicate elements. The class ArrayList retains the order of insertion and is not synchronized. It is possible to access randomly the elements stored in the ArrayList class.  

Take the instance below. 

importjava.util.*;   
class TestJavaArrayList{   
   public static voidmain(String args[]){   
ArrayList<String> myList=new ArrayList<String>(); 
   myList.add("Ashish"); 
myList.add("Rahul");   
   myList.add("Shivam");   
myList.add("Montu");   
Iterator itr= myList.iterator();   
   while(itr.hasNext()){   
System.out.println(itr.next());   
}   
}   
}   

Output of above program: 

Ashish 
Rahul 
Shivam 
Montu 

LinkedList 

The Collection interface is implemented by LinkedList. It internally utilizes a dual-linked list to store the elements. The duplicate elements can be stored. It keeps the order of insertion and is not synchronized. The manipulation is quick in LinkedList because no shifting is necessary. 

Take the instance below. 

  import java.util.*;   
publicclass TestJavaLinkedList{   
  public static void main(String args[]){   
LinkedList<String> linkedList =new LinkedList<String>();   
  linkedList.add("Ashish");   
linkedList.add("Rahul");   
  linkedList.add("Shivam");   
linkedList.add("Montu");   
  Iterator<String> itr= linkedList.iterator();   
while(itr.hasNext()){   
System.out.println(itr.next());   
}   
  }   
}   

Output: 

Ashish 
Rahul 
Shivam 
Montu 

Vector 

To store the information elements, the Vector utilizes a dynamic array. It's ArrayList-like. It is synchronized, however, and includes many methods that are not part of the framework for collection. 

Take the instance below. 

 import java.util.*;   
publicclass TestJavaCollection3{   
  public static void main(String args[]){   
Vector<String> myVector=new Vector<String>();   
  myVector.add("Sneha");   
myVector.add("Priya");   
  myVector.add("Sonia");   
myVector.add("Preeti");   
  Iterator<String> itr= myVector.iterator();   
while(itr.hasNext()){   
  System.out.println(itr.next());   
}   
 }   
}   

Output:

Sneha 
Priya 
Sonia 
Preeti 

Stack 

The stack is Vector's subclass. It uses the data structure of the last-in-first-out, i.e. Stack. The stack includes all Vector class methods and also offers its techniques such as boolean push), (boolean peek), (boolean push(object o) defining its characteristics. 

Take the instance below. 

  import java.util.*;   
publicclass TestJavaCollection4{   
  public static void main(String args[]){   
Stack<String> myStack = new Stack<String>();   
  myStack.push("Ashish");   
myStack.push("Priya");   
  myStack.push("Mohan");   
myStack.push("Sneha");   
  myStack.push("Kangana");   
myStack.pop();   
  Iterator<String> itr= myStack.iterator();   
while(itr.hasNext()){   
  System.out.println(itr.next());   
}   
  }   
}   

Output: 

Ashish 
Priya 
Mohan 
Sneha 

Queue Interface in java 

Queue interface keeps the first-in-first-out order. It can be described as an ordered list used to keep the processed elements. There are different classes that implement the Queue interface, such as PriorityQueue, Deque, and ArrayDeque. 

Take the instance below. 

Queue<Integer> queue1 = new PriorityQueue<Integer>();   
Queue<Integer> queue2 = new ArrayDeque<Integer>();   

PriorityQueue 

The Queue interface is implemented by the PriorityQueue class. It contains the components or items to be processed according to their priorities. PriorityQueue does not permit the storage of null values in the queue. 

Take the instance below. 

  importjava.util.*;   
publicclass TestJavaQueue{   
  public static void main(String args[]){   
PriorityQueue<String> myQueue=new PriorityQueue<String>();   
  myQueue.add("Ashish ");   
myQueue.add("Vijay Raj");   
  myQueue.add("JaiShankar");   
myQueue.add("Raj");   
  System.out.println("Head element:"+ myQueue.element());   
System.out.println("Head element:"+ myQueue.peek());   
  System.out.println("iterating the queue elements:");   
Iterator itr= myQueue.iterator();   
  while(itr.hasNext()){   
System.out.println(itr.next());   
  }   
myQueue.remove();   
  myQueue.poll();   
System.out.println("after removing 2 elements:");   
  Iterator<String> itr2=queue.iterator();   
while(itr2.hasNext()){   
  System.out.println(itr2.next());   
}   
 }   
}   

Output: 

Head element:Ashish 
Head element:Ashish 
iterating the queue elements: 
Ashish 
Raj 
JaiShankar 
Vijay Raj 
after removing 2 elements: 
JaiShankar 
Vijay Raj 

Deque Interface 

Deque interface expands the interface to the queue. We can delete and add the components from both sides in Deque. Deque is a double-ended queue that allows us to execute both ends of the activities. 

Take the instance below. 

Deque deque  = new ArrayDeque();  

ArrayDeque 

The Deque interface is implemented by the ArrayDeque class. It makes it easier for us to use the Deque. Unlike queue, from both ends we can add or delete the components. ArrayDeque is quicker than ArrayList and Stack and has no limitations on capability.Take the instance below. 

  import java.util.*;   
publicclass TestJavaQueue{   
  public static voidmain(String[] args) {   
  Deque<String> myDeque = new ArrayDeque<String>();   
myDeque.add("Priya");   
  myDeque.add("Sonia");   
myDeque.add("Rahul");   
for (String str : myDeque) {   
  System.out.println(str);   
}   
 }   
}   

Output

Priya 
Sonia 
Rahul 

Set Interface in java 

The Java Set Interface package includes java.util. It expands the interface for the collection. It represents the unordered set of elements that do not allow the duplicate items to be stored. We can store a null value in Set as much as possible. HashSet, LinkedHashSet, and TreeSet implements the set. 

Take the instance below. 

Set<data-type> set1 = new HashSet<data-type>();   
Set<data-type> set2 = new LinkedHashSet<data-type>();   
Set<data-type> set3 = new TreeSet<data-type>();   

HashSet 

Class HashSet implements Interface Set. It is the set that utilizes a storage hash table. Hashing is used to store HashSet components. It includes distinctive elements. 

Take the instance below. 

  import java.util.*;   
publicclassTestJavaSet{   
  public static void main(String args[]){   
  HashSet<String> mySet=new HashSet<String>();   
mySet.add("Ashish");   
  mySet.add("Priya");   
mySet.add("Hemu");   
  Iterator<String> itr= Myset.iterator();   
while(itr.hasNext()){   
  System.out.println(itr.next());   
}   
  }   
}   

Output: 

Ashish 
Priya 
Hemu 

LinkedHashSet 

LinkedHashSet class reflects Set Interface's LinkedList application. It expands the class of HashSet and implements the interface set. It also includes distinctive components such as HashSet. It keeps the order of insertion and allows null components. 

Take the instance below. 

  import java.util.*;   
publicclass TestJavaSet{   
  public static voidmain(String args[]){   
LinkedHashSet<String> Myset=new LinkedHashSet<String>();   
  Myset.add("Sonia");   
Myset.add("Kangana");   
  Myset.add("Hemu");   
  Iterator<String> itr= Myset.iterator();   
while(itr.hasNext()){   
  System.out.println(itr.next());   
}   
  }   
}   

Output

Sonia 
Kangana 
Hemu 

SortedSet Interface 

SortedSet is the alternate of a Set interface that provides a total ordering on its elements. The elements of the SortedSet are arranged in the increasing (ascending) order. The SortedSet provides additional methods that inhibit the natural ordering of the elements. 

Take the instance below. 

SortedSet<data-type> set = new TreeSet data-type>();   

TreeSet 

SortedSet is the Set interface alternative that offers a complete ordering of its components. The SortedSet components are arranged in the rising (ascending) order. The SortedSet offers extra methods that inhibit the elements ' natural order. 

Take the instance below. 

  import java.util.*;   
publicclassTestJavaSet{   
  public static void main(String args[]){   
  TreeSet<String> mySet=new TreeSet<String>();   
mySet.add("Raju");   
  mySet.add("Vijay");   
  mySet.add("Ashish");   
  Iterator<String> itr= mySet.iterator();   
while(itr.hasNext()){   
  System.out.println(itr.next());   
}   
  }   
}   

Output: 

Ashish 
Raju 
Vijay 

Leave a Reply

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

Comments

protective orders in virginia

Keep sharing blogs like this one; they are quite good. You have given everyone in this blog access to a wealth of information.

Johnfrance

Thank you for your valuable information.

sam

Thank you for this wonderful article!

Belay Abera

This article was really helpful to me, thank you!

dfsdf

super article!