Thursday, 10 December 2015

How to Remove Element from Collection Using Iterator ?

package com.JavaInterview.Programs;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * Remove Element from Collection Using Iterator
 * 
 * 
 */
public class RemoveElementItrJIP {

 public static void main(String args[]) {
  List<String> JipList = new ArrayList<String>();
  JipList.add("ABC");
  JipList.add("Ram");
  JipList.add("DEF");
  JipList.add("123");
  System.out.println("The original Collection " + JipList);
  Iterator<String> itr = JipList.iterator();
  int count = 0;
  Scanner input = new Scanner(System.in);
  System.out.println("Please enter the element that you want to Remove :");
  String rmvElement = input.next();
  while (itr.hasNext()) {
   if (rmvElement.equals(itr.next())) {
    itr.remove();
    count++;
   }
  }

  if (count > 0) {
   System.out.println("After Remove:" + JipList);
  } else {
   System.out.println("No such element Exist!");

  }

  input.close();
 }
}

Output :
The original Collection [ABC, Ram, DEF, 123]
Please enter the element that you want to Remove :
123
After Remove:[ABC, Ram, DEF]


How to Search key/value in Hashtable?

package com.JavaInterview.Programs;

import java.util.Hashtable;
import java.util.Map;
import java.util.Scanner;


/**
 * Search key/value Hashtable
 * 
 * 
 */
public class HashtableKeySearch {

 public static void main(String args[]) {
  // Create Hashtable Instance
  Hashtable<Integer, String> JipHT = new Hashtable<Integer, String>();
  // Adding element to Hashtable
  JipHT.put(1, "first");
  JipHT.put(2, "second");
  JipHT.put(3, "third");

  // Print the Hashtable
  System.out.println("Original Hashtable :" + JipHT);

  Scanner input = new Scanner(System.in);
  // Search element by Key
  System.out.println("Please enter the key that you want to search :");
  int key = input.nextInt();

  if (JipHT.containsKey(key)) {
   System.out.println("The Hashtable contains the key " + key + " with value : " + JipHT.get(key));
  } else {
   System.out.println("The Hashtable dosn't contains the key");

  }
  // Search Element by Value
  Scanner input1 = new Scanner(System.in);
  Integer key1 = null;
  System.out.println("Please enter the value that you want to search :");
  String str = input1.next();
  for (Map.Entry entry : JipHT.entrySet()) {
   if (str.equals(entry.getValue())) {
    key1 = (Integer) entry.getKey();
    break;
   }
  }

  System.out.println("The key value from Hashtable for value " + str + "is :" + key1);
  input.close();
  input1.close();
 }
}

Output :
Original Hashtable :{3=third, 2=second, 1=first}
Please enter the key that you want to search :
3
The Hashtable contains the key 3 with value : third
Please enter the value that you want to search :
first
The key value from Hashtable for value firstis :1

Perform Basic Operation on Hashtable

package com.JavaInterview.Programs;

import java.util.Hashtable;
import java.util.Set;

/**
 * Perform Basic Operation on Hashtable
 * 
 * 
 */
public class HashTableBasicJIP {
 public static void main(String args[]) {

  // Create Hashtable Instance
  Hashtable<Integer, String> JipHT = new Hashtable<Integer, String>();
  // Adding element to Hashtable
  JipHT.put(1, "first");
  JipHT.put(2, "second");
  JipHT.put(3, "third");

  // Print the Hashtable
  System.out.println("Original Hashtable :"+JipHT);

  // Adding element by duplicate key
  JipHT.put(2, "six");
  System.out.println("Hashtable post duplicate key" + JipHT);

  // Adding element by duplicate value
  JipHT.put(33, "third");
  System.out.println("Hashtable post duplicate value" + JipHT);

  // getting value for given key
  System.out.println("value for the key '1'" + JipHT.get(1));

  // Check if Hashtable is empty
  System.out.println("Is Hashtable empty:" + JipHT.isEmpty());

  // Remove element from Hashtable
  JipHT.remove(1);
  System.out.println("Hashtable post removal of its elemnet " + JipHT);

  // Size of the Hashtable
  System.out.println("Size of Hashtable:" + JipHT.size());

  //Iterate through the Hashtable
  Set<Integer> keys=JipHT.keySet();
  for(Integer key:keys)
  {
   System.out.println("Value of "+key +" is "+JipHT.get(key));
  }



 }
}

Output:
Original Hashtable :{3=third, 2=second, 1=first}
Hashtable post duplicate key{3=third, 2=six, 1=first}
Hashtable post duplicate value{3=third, 2=six, 1=first, 33=third}
value for the key '1'first
Is Hashtable empty:false
Hashtable post removal of its elemnet {3=third, 2=six, 33=third}
Size of Hashtable:3
Value of 3 is third
Value of 2 is six
Value of 33 is third



How to Copy Map content to another Hashtable ?

package com.JavaInterview.Programs;

import java.util.Hashtable;
import java.util.HashMap;

/**
 * Copy Map content to another Hashtable
 * 
 * 
 */
public class CopyMapToHashTable {

 public static void main(String args[]) {
  // Create Hashtable Instance
  Hashtable<Integer, String> JipHT = new Hashtable<Integer, String>();
  // Adding element to Hashtable
  JipHT.put(1, "first");
  JipHT.put(2, "second");
  JipHT.put(3, "third");

  // Print the Hashtable
  System.out.println("Original Hashtable :" + JipHT);

  // Create HashMap
  HashMap<Integer, String> JipHM = new HashMap<Integer, String>();
  JipHM.put(5, "five");
  JipHM.put(6, "six");

  // Copying Map to Hashtable
  JipHT.putAll(JipHM);

  // Print the Hashtable
  System.out.println("Hashtable after copying :" + JipHT);

 }
}

Output:
Original Hashtable :{3=third, 2=second, 1=first}
Hashtable after copying :{6=six, 5=five, 3=third, 2=second, 1=first}


How to Iterate trough Collection Objects ?

package com.JavaInterview.Programs;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Iterate trough Collection Objects
 * 
 * @author:http://javainterviewprograms.blogspot.in/
 * 
 */
public class CollectionIteratorJIP {

public static void main(String args[])
{
List<String> JipList=new ArrayList<String>();
JipList.add("ABC");
JipList.add("Ram");
JipList.add("DEF");
JipList.add("123");

Iterator<String> itr=JipList.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}


Output:
ABC
Ram
DEF
123

Difference between final, finally and finalize()?

final :Final is a keyword. Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final ...