Wednesday, 29 August 2018

Program on Collections Framework in Java

                     In this post, we will see a sample program which uses Collections framework in Java.


                      To find What is Collections Framework, What is the necessity of Collections framework in Java and Structure of Collections framework, please check this link: https://www.comrevo.com/2018/07/What-is-Collections-Framework-in-Java.html

Program (CollectionsDemo.java)
import java.util.*;
public class CollectionsDemo {

   public static void main(String[] args) {
      // ArrayList 
      List a1 = new ArrayList();
      a1.add("Rehan");
      a1.add("Rehan");
      a1.add("Kaira");
      System.out.println(" ArrayList Elements");
      System.out.print("\t" + a1);

      // LinkedList
      List l1 = new LinkedList();
      l1.add("Rehan");
      l1.add("Rehan");
      l1.add("Kaira");
      System.out.println();
      System.out.println(" LinkedList Elements");
      System.out.print("\t" + l1);

      // HashSet
      Set s1 = new HashSet(); 
      s1.add("Rehan");
      s1.add("Kaira");
      s1.add("Sara");
      System.out.println();
      System.out.println(" HashSet Elements");
      System.out.print("\t" + s1);
      System.out.println();

      // LinkedHashSet
      Set lhs1 = new LinkedHashSet(); 
      lhs1.add("Rehan");
      lhs1.add("Kaira");
      lhs1.add("Sara");
      System.out.println();
      System.out.println(" LinkedHashSet Elements");
      System.out.print("\t" + lhs1);
      System.out.println();

      // TreeSet
      Set t1 = new TreeSet(); 
      t1.add("Rehan");
      t1.add("Kaira");
      t1.add("Sara");
      System.out.println();
      System.out.println(" TreeSet Elements");
      System.out.print("\t" + t1);
      System.out.println();

      // Queue
      Queue q1=new PriorityQueue();  
      q1.add("Rehan");  
      q1.add("Kaira");  
      q1.add("Sara");  
      q1.add("Parag");  
      q1.add("Soniya");  
      System.out.println(" Queue Elements");
      System.out.print("\t" + q1);
      System.out.print("\n");
      System.out.println("head:"+q1.element());
      System.out.print("\n"); 

      //Deque
      Deque d1=new ArrayDeque();  
      d1.add("Rehan");  
      d1.add("Kaira");  
      d1.add("Sara");  
      d1.offerFirst("Parag"); 
      d1.offerLast("Soniya");
      System.out.println(" Deque Elements");
      System.out.print("\t" + d1);
      System.out.print("\n");
      System.out.println("head:"+d1.element()); 

      // HashMap
      Map m1 = new HashMap(); 
      m1.put("Rehan", "8");
      m1.put("Kaira", "31");
      m1.put("Sara", "12");
      m1.put("Parag", "14");
      System.out.println();
      System.out.println(" HashMap Elements");
      System.out.print("\t" + m1);
      System.out.print("\n");

      // LinkedHashMap
      Map lhm1 = new LinkedHashMap(); 
      lhm1.put("Rehan", "8");
      lhm1.put("Kaira", "31");
      lhm1.put("Sara", "12");
      lhm1.put("Parag", "14");
      System.out.println();
      System.out.println(" LinkedHashMap Elements");
      System.out.print("\t" + lhm1);
      System.out.print("\n");

      // TreeMap
      Map tm1 = new TreeMap(); 
      tm1.put("Rehan", "8");
      tm1.put("Kaira", "31");
      tm1.put("Sara", "12");
      tm1.put("Parag", "14");
      System.out.println();
      System.out.println(" TreeMap Elements");
      System.out.print("\t" + tm1);
      System.out.print("\n");
   }
}



  

Output: 
parag@parag-Inspiron-N4010:~/Desktop/programs$ javac CollectionsDemo.java 
parag@parag-Inspiron-N4010:~/Desktop/programs$ java CollectionsDemo
 ArrayList Elements
[Rehan, Rehan, Kaira] 

 LinkedList Elements
[Rehan, Rehan, Kaira] 

 HashSet Elements
[Rehan, Sara, Kaira]

 LinkedHashSet Elements
[Rehan, Kaira, Sara]

 TreeSet Elements
[Kaira, Rehan, Sara] 

 Queue Elements
[Kaira, Parag, Sara, Rehan, Soniya]
head:Kaira

 Deque Elements
[Parag, Rehan, Kaira, Sara, Soniya]
head:Parag

 HashMap Elements
{Rehan=8, Parag=14, Sara=12, Kaira=31}

 LinkedHashMap Elements
{Rehan=8, Kaira=31, Sara=12, Parag=14}

 TreeMap Elements
{Kaira=31, Parag=14, Rehan=8, Sara=12}

  



No comments:

Post a comment