Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to use Vector in Java with example ?.

Program to demonstrate how to implement Vector class in java.util.*; package along with its operations :

package com.hubberspot.collections.example;

import java.util.*;

public class VectorExample 
{
  public static void main(String args[]) 
  {
    Vector vector = new Vector(3, 2);
      
    System.out.println("Initial size: " + vector.size());
    System.out.println("Initial capacity: " +
      
    vector.capacity());
      
    vector.addElement(new Integer(3));
    vector.addElement(new Integer(4));
    vector.addElement(new Integer(5));
    vector.addElement(new Integer(6));
      
    System.out.println("Capacity after adding 4 elements: " +
          vector.capacity());

    vector.addElement(new Double(6.35));
      
    System.out.println("Current capacity: " +
          vector.capacity());
      
    vector.addElement(new Double(7.98));
    vector.addElement(new Integer(8));
     
    System.out.println("Current capacity: " +
          vector.capacity());
      
    vector.addElement(new Float(10.4));
    vector.addElement(new Integer(11));
      
    System.out.println("Current capacity: " +
      vector.capacity());
      
    System.out.println("First element: " +
         vector.firstElement());
      
    System.out.println("Last element: " +
         vector.lastElement());
     
    System.out.println("\nElements in vector: " + vector);
      
   }
}

Output of the program :


How to implement ListIterator Interface in Java Collections ?.

Program to demonstrate implementation of ListIterator Interface in Java Collections

package com.hubberspot.collections.example;

import java.util.*;

public class ListIteratorExample
{
 public static void main(String[] args)
 {
  LinkedList list = new LinkedList();
  
  list.add("One");
  list.add("Two");
  list.add("Three");
  list.add("Four");
  
  ListIterator iterator = list.listIterator();
  
  while(iterator.hasNext())
  {     
 String element = (String)iterator.next();
 System.out.println(element);
    iterator.set("Number " + element);
  }  
 
  
  System.out.println("Traversing Backwards : ");
 
  while(iterator.hasPrevious())
    System.out.println(iterator.previous()); 
  }
 
}

Output of the program :


How to perform operations of Union and Intersection on two or more LinkedList in Java ?.

Program to demonstrate Union and Intersection of two LinkedList in Java

package com.hubberspot.collections.example;

import java.util.*;
import java.io.*;

public class LinkedListOperationsDemo
{
  static BufferedReader br = new BufferedReader
   (new InputStreamReader(System.in));

  public static void main(String args[]) throws IOException
  {
    LinkedList list1 = new LinkedList();
    LinkedList list2 = new LinkedList();
    LinkedList list3 = new LinkedList();
    LinkedList list4 = new LinkedList();
   
    int num1, num2;

    System.out.print("Enter no.of Elements in list 1:");
  
    num1 = Integer.parseInt(br.readLine());

    System.out.println("Enter Elements of list 1");
  
    for(int i=1; i <= num1; i++)
    {
      System.out.print("Enter Element "+i+": ");
      String element = br.readLine();
 
      if(!list1.contains(element))
 list1.add(element);
    }                                

    System.out.print("Enter no.of Elements in list 2:");
  
    num2 = Integer.parseInt(br.readLine());
  
    System.out.println("Enter Elements of list 2");
  
    for(int i=1; i <= num2; i++)
    {
      System.out.print("Enter Element "+i+": ");
      String element = br.readLine();
 
      if(!list2.contains(element))
 list2.add(element);
    }                                

    Collections.sort(list1);
    Collections.sort(list2);

    System.out.println("List 1:"+list1);
    System.out.println("List 2:"+list2);
     
    for(int i=0; i < list1.size(); i++)
      list3.add(list1.get(i));
  
    for(int i=0; i < list2.size(); i++)
      if(!list1.contains(list2.get(i)))
 list3.add(list2.get(i));
  
    System.out.println("Union: "+list3);
  
    for(int i=0; i < list1.size(); i++)
       list4.add(list1.get(i));
  
    list4.retainAll(list2);
  
    System.out.println("Intersection: "+list4);

   }
}
Output of the program : 

 
 
© 2021 Learn Java by Examples Template by Hubberspot