Object Oriented Programming-I (LAB Session :- 12)


 Java Programming with Collection


    1.    Write a Java program to display use of below methods from Collection interface. (Create collections like ArrayList and show output of below methods. Understand the use of those methods).

Methods: add(), addAll(), clear(), clearAll(), remove(), removeAll(), contains(), retainAll()

CODE :

import java.util.*;

public class ArrayListExample { 
    public static void main(String[] args) { 
        ArrayList<String> collection1
            = new ArrayList<String>(); 
       collection1.add("Bhavnagar");
       collection1.add("Surat");
       collection1.add("Bhuj");
       collection1.add("Ahemadabad");
      System.out.println("\n Using add() method \n "+collection1);
  
    ArrayList<String> list1
            = new ArrayList<String>(); 
      list1.add("Jamnagar");
      list1.add("Virpur");
      list1.add("Jetpur");
      collection1.addAll(list1);
      System.out.println("\n Using addAll() method \n "+collection1);
            
       collection1.clear();
       System.out.println("\n using clear() method \n "+collection1);    
       
       ArrayList<String> list2
            = new ArrayList<String>(); 
       list2.add("Bharuch");
       list2.add("Aanand");
       list2.add("Morbi");
       list2.add("Porbandar");
  
       list2.remove("Morbi");
       System.out.println("\n Using remove() method \n "+list2);
       
       ArrayList<String> list3
            = new ArrayList<String>(); 
       list3.add("Rajkot");
       list3.add("Kheda");
       
       list2.addAll(list3);
       list2.removeAll(list3);
       System.out.println("\n Using removeAll() method \n "+list2);
 
       System.out.println("\n Using contain() method \n "+list2.contains(list3));
       System.out.println("\n "+list2.contains("Aanand"));
 
        ArrayList<String> list4
            = new ArrayList<String>(); 
        list4.add("Aanand");
        list4.add("Rajkot");
        list4.add("Tapi");
        list4.retainAll(list3);
        System.out.println("\n Using retainAll() method \n "+list4); 
     }
 }

OUTPUT :



2. Create an Iterator for above any one of the ArrayList created in

above program. Display the use oftraversing the list through Iterat

or.

CODE :

import java.util.*; 
public class IteratorEx { 
    public static void main(String[] args) { 
        ArrayList<String> list = new ArrayList<>(); 
        list.add("Junagadh"); 
        list.add("Rajkot"); 
        list.add("Botad"); 
        list.add("Bhavnagar"); 
        list.add("Bhuj"); 
        list.add("Morbi"); 
        
        System.out.println("List is: \n"+ list); 
        Iterator<String> iter = list.iterator(); 
        System.out.println("\nThe iterator values of list are: "); 

        while (iter.hasNext()) { 
            System.out.println(iter.next() + " "); 

        } 
    } 
}

OUTPUT :





3.Write a Java program to create a Queue and display use of below

methods.

Methods: offer(), poll(), peek(), remove()

CODE :

import java.util.*;
public class Example {
   public static void main(String[] args) {
      
      Queue<String> q = new LinkedList<String>();
      System.out.println("Using offer() :-" + q);
      q.offer("Rajkot");
      System.out.println(q);
      q.offer("Rajula");
      System.out.println(q);
      q.offer("Tapi");
      System.out.println(q);
      q.offer("Bharuch");
      System.out.println(q);
      q.offer("Daman");
      System.out.println(q);   
      System.out.println("The queue is: " + q);
      System.out.println( "\nUsing peek() :-\nPeek is "+q.peek());
      
      System.out.println("\nUsing poll() :- ");
      q.poll();
      System.out.println("The queue is: " + q);
      System.out.println( "Peek is "+q.peek());
      
      System.out.println("\nUsing remove() :- ");
      String s = "Tapi";
      q.remove(s);
      System.out.println("Tapi is removed from queue ");
      System.out.println("The queue is: " + q);
      System.out.println( "Peek is "+q.peek());
}
}

OUTPUT :




4.Write a program to use below methods of java.util.Collections.

Method: sort(), binarySearch(), shuffle(), reverse(), copy(),

max(), min(), disjoint(), frequency()

CODE :
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;

public class CollectionsDemo {

    public static void main(String[] args) {
        ArrayList<String> List = new ArrayList<String>();
        List.add("Rajkot");
        List.add("Surat");
        List.add("Bhuj");
        List.add("Cat");
        List.add("Ajay");
        List.add("Rad");
        List.add("Surat");
        System.out.println("List :- \n" + List);

        Collections.sort(List);
        System.out.println("\nUsing sort() method :-\n" + List);
        System.out.println("\nUsing sort() method :-");
        int index = Collections.binarySearch(List, "Cat");
        System.out.println("Index of Cat :- "+index);
        index = Collections.binarySearch(List, "bat");
        System.out.println("Index of bat :- "+index);

       Collections.shuffle(List);
        System.out.println("\nUsing shuffle() mehod :-\n" + List);
        
        Collections.reverse(List);
        System.out.println("\nUsing reverse() method :-\n" + List);
  
     System.out.println("\nUsing max() method :-\n"+Collections.max(List));
     System.out.println("\nUsing min() method :-\n"+Collections.min(List));
     
     System.out.println("\nUsing frequency() method :-");
     System.out.println("Frequency of Neeraj is "+
                + Collections.frequency(List, "Neeraj"));
    System.out.println("Frequency of Surat is "+
                + Collections.frequency(List, "Surat"));
    
    ArrayList<String> List2 = new ArrayList<String>();
        List2.add("Bhavnagar");
        List2.add("Valsad");
        List2.add("Rocket");
    System.out.println("\nList2 :- \n" + List2);
    System.out.println("\nUsing disjoint() method :-");
    System.out.println("List1 and List2 are disjoint :- " + Collections.disjoint(List, List2)); 
       
      Collections.copy(List, List2);
      System.out.println("\nUsing copy() method :-");      System.out.println("\nList :- " + List);
      System.out.println("\nList2 :- " + List2);
      System.out.println("\nList1 and List2 are disjoint :- " + Collections.disjoint(List2,List)); 
    }
}

OUTPUT :





DOWNLOAD PDF HERE....





No comments:

Post a Comment