|  Home   |  Contact Us   |  Online Exam   |  Tech World   |  Jobs    |  Member Login
Java Interview Questions
Jdbc Interview Questions
Servlet Interview Questions
Jsp Interview Questions
Struts Interview Questions
Ajax Interview Questions
Webservice Interview Questions
Designpattern Interview Questions
J2me Interview Questions
EJB Interview Questions
Spring Interview Questions
Hibernet Interview Questions
Enterprise IT Training
Java Program Examples
Ask Technical Expert!
Write Online Exam and Get Job Opportunity Here!
        Java Examples          
OOPS  |   Exception  |   RMI  |   String  |   Thread  |   io  |   Util  |   Net  |   AWT  |   JDBC  |  

UTIL PROGRAM::
List Program:-:
1.Vector Program
     import java.util.*;
     public class VectorEx {
     public static void main(String[] args) {
     Vector vec = new Vector();
     vec.addElement(new Integer(10));
     vec.addElement(new Double(10.236));
     vec.addElement(new Float(10.56));
     vec.addElement(new Long(100));
     vec.addElement(new Boolean("True"));
     System.out.println("The Contents Of the Vector :");
     System.out.println();
     System.out.println("The Capacity Of the Vector is :" +vec.capacity());
     System.out.println();
     Enumeration enum = vec.elements();
     while(enum.hasMoreElements())
     System.out.print(enum.nextElement() + " ");
     System.out.println();
     System.out.println();
     Object s[]=new Object[vec.size()];
     vec.copyInto(s);
     System.out.print("Vector Elemenets Copied into String Array -> ");
     for(int i=0;i<s.length;i++)
     System.out.print( " : " + s[i]);
     System.out.println();
     System.out.println();
     System.out.println("The First Elemenet :" +vec.firstElement());
     System.out.println();
     System.out.println("The Last Element :" +vec.lastElement());
     System.out.println();
     }
     }
2.ArrayList Program
     import java.util.*;
     public class ArrayListEx {
     public static void main(String[] args) {
     ArrayList ar1 = new ArrayList();
     ar1.add("Z");
     ar1.add("A");
     ar1.add("D");
     ar1.add("C");
     ar1.add("B");
     ar1.add(0,"First");
     System.out.println("The Contents Of the Array List is in insertion order :");
     System.out.println();
     System.out.println("The Size Of the Array List is :" +ar1.size());
     System.out.println();
     System.out.println("The Contents Of the Array List in ar1 :" +ar1);
     System.out.println();
     Collection ar2 =new ArrayList();
     ar2.addAll(ar1);
     System.out.println("The Contents Of the Array List in ar2 :" +ar2);
     System.out.println();
     System.out.println("ar1 contains all elements in ar2 : " +ar1.containsAll(ar2));
     System.out.println();
     System.out.println("ar1 contains object 'First' : " +ar1.contains("First"));
     System.out.println();
     System.out.println("ar1 equals to ar2 : " +ar1.equals(ar2));
     System.out.println();
     ar2.remove("First");
     System.out.println("After removing 'First' in ar2 :" +ar2);
     System.out.println();
     System.out.println("ar2 is Empty :" +ar2.isEmpty());
     System.out.println();
     ar2.add("New in ar2");
     System.out.println("Elements in ar2 :" +ar2);
     System.out.println();
     ar2.retainAll(ar1);
     System.out.println("After retaining all objects in [ar1] ar2 is : " +ar2);
     System.out.println();
     ar2.add("New in ar2");
     System.out.println("Object 'New in ar2 ' is added in ar2 :");
     System.out.println();
     ar2.removeAll(ar1);
     System.out.println("After removing all objects in [ar1] ar2 is : " +ar2);
     System.out.println();
     }
     }
3.LinkedList Program
      // LinkedList Example Index ,supports Duplicate
     import java.util.*;
     public class LinkedListEx {
     public static void main(String[] args) {
     LinkedList list = new LinkedList();
     list.add("Z");
     list.add("A");
     list.add("D");
     list.add("C");
     list.add("B");
     list.addFirst("First");
     list.addLast("Last");
     System.out.println("The Contents Of the LinkedList is in insertion order : Supports Duplicate :");
     System.out.println();
     System.out.println("The Size Of the LinkedList is :" +list.size());
     System.out.println();
     System.out.println("The Contents Of the LinkedList in list :" +list);
     System.out.println();
     list.remove(4);
     list.remove("A");
     System.out.println("After Removing Object 'A' and object at position '4' : "+list);
     System.out.println();
     list.removeFirst();
     list.removeLast();
     System.out.println("After Removing First and Last Object : " + list);
     System.out.println();
     }
     }
Collection Program:-:
1.HashSet Program
     import java.util.*;
     public class HashSetEx {
     public static void main(String[] args) {
     HashSet hs1 = new HashSet();
     hs1.add("Z");
     hs1.add("A");
     hs1.add("D");
     hs1.add("C");
     hs1.add("B");
     hs1.add("First");
     System.out.println("The Contents Of the Hash Set Doestnot Support Duplicate : ");
     System.out.println();
     System.out.println("The Size Of the HashSet is :" +hs1.size());
     System.out.println();
     System.out.println("The Contents Of the HashSet in hs1 :" +hs1);
     System.out.println();
     Collection hs2 =new HashSet();
     hs2.addAll(hs1);
     System.out.println("The Contents Of the HashSet in hs2 :" +hs2);
     System.out.println();
     System.out.println("hs1 contains all elements in hs2 : " +hs1.containsAll(hs2));
     System.out.println();
     System.out.println("hs1 contains object 'First' : " +hs1.contains("First"));
     System.out.println();
     System.out.println("hs1 equals to h2 : " +hs1.equals(hs2));
     System.out.println();
     hs2.remove("First");
     System.out.println("After removing 'First' in hs2 :" +hs2);
     System.out.println();
     System.out.println("hs2 is Empty :" +hs2.isEmpty());
     System.out.println();
     hs2.add("New in hs2");
     System.out.println("Elements in hs2 :" +hs2);
     System.out.println();
     hs2.retainAll(hs1);
     System.out.println("After retaining all objects in [hs1] hs2 is : " +hs2);
     System.out.println();
     hs2.add("New in hs2");
     System.out.println("Object 'New in hs2 ' is added in hs2 :");
     System.out.println();
     hs2.removeAll(hs1);
     System.out.println("After removing all objects in [hs1] hs2 is : " +hs2);
     System.out.println();
     }
     }
2.Tree Set Program
      // As well as Tree Set
     import java.util.*;
     public class TreeSetEx {
     public static void main(String[] args) {
     TreeSet ts = new TreeSet();
     ts.add("Z");
     ts.add("A");
     ts.add("D");
     ts.add("C");
     ts.add("B");
     ts.add("First");
     System.out.println("The Contents Of the TreeSet Doestnot Support Duplicate : ");
     System.out.println();
     System.out.println("The Size Of the TreeSet is :" +ts.size());
     System.out.println();
     System.out.println("The Contents Of the TreeSet in ts :" +ts);
     System.out.println();
     System.out.println("Get the First Object in TreeSet in ts :" +ts.first());
     System.out.println();
     System.out.println("Get the Last Object in TreeSet in ts :" +ts.last());
     System.out.println();
     System.out.println("Get HeadSet in TreeSet in ts :" +ts.headSet("Z"));
     System.out.println();
     System.out.println("Get SubSet in TreeSet in ts :" +ts.subSet("A","D"));
     System.out.println();
     System.out.println("Get TailSet in TreeSet in ts :" +ts.tailSet("D"));
     System.out.println();
     }
     }
Mapping Program:-:
1.Map Program
      import java.util.*;
     public class MapEx {
     public static void main(String[] args) {
     HashMap map = new HashMap();
     map.put("Kannan","Chennai");
     map.put("Elangovan","Bangalore");
     map.put("Emkay","Kolkatta");
     map.put("Dhoni","Bombay");
     map.put("Pathan","Delhi");
     System.out.println("The Contents Of the Map : ");
     System.out.println();
     System.out.println("The Size Of the Map is :" +map.size());
     System.out.println();
     Set mapset = map.entrySet();
     Iterator itr = mapset.iterator();
     while(itr.hasNext()) {
     Map.Entry me = (Map.Entry)itr.next();
     System.out.print(me.getKey() +" ");
     System.out.println(me.getValue());
     }
     System.out.println();
     System.out.println("The Map Contains Key [Pathan] :" +map.containsKey("Pathan"));
     System.out.println();
     System.out.println("The Map Contains Value [Chennai] :" +map.containsValue("Chennai"));
     System.out.println();
     System.out.println("Get the Value for Key [Pathan] :" +map.get("Pathan"));
     System.out.println();
     System.out.println("Remove Object contains Key [Kannan] :" +map.remove("Kannan"));
     System.out.println();
     Set kset =map.keySet();
     System.out.print("Getting Keys From Map :" +kset);
     System.out.println();
     System.out.println();
     Collection vcol =map.values();
     System.out.print("Getting Values From Map :" +vcol);
     System.out.println();
     System.out.println();
     map.clear();
     System.out.println("Clear the Map : []");
     System.out.println();
     System.out.println("Map is Empty :" +map.isEmpty());
     System.out.println();
     }
     }
2.TreeMap Program
      // Storing Key/Value pairs itis Integrated with Collections
     import java.util.*;
     public class TreeMapEx {
     public static void main(String[] args) {
     TreeMap tm = new TreeMap();
     tm.put("Kannan","Chennai");
     tm.put("Sachin","Bombay");
     tm.put("Elangovan","Bangalore"); tm.put("Ram","Vellore");
     tm.put("Pathan","Delhi");
     tm.put("Ganguly","Kolkata");
     System.out.println("The Contents Of the SortedMap [Tree Map] : ");
     System.out.println();
     System.out.println("The Size Of the Tree Map is :" +tm.size());
     System.out.println();
     Set tmapset = tm.entrySet();
     Iterator itr = tmapset.iterator();while(itr.hasNext()) {
     Map.Entry me = (Map.Entry)itr.next();
     System.out.print(me.getKey() +" ");
     System.out.println(me.getValue());
     }
     System.out.println();
     System.out.println("First Key in the TreeMap :" +tm.firstKey());
     System.out.println();
     System.out.println("Last Key in the TreeMap :" +tm.lastKey());
     System.out.println();System.out.println("HeadMap in the TreeMap :" +tm.headMap("Pathan"));
     System.out.println();      System.out.println("SubMap in the TreeMap :
     " +tm.subMap("Ganguly","Sachin"));
     System.out.println();
     System.out.println("TailMap in the TreeMap :" +tm.tailMap("Kannan"));
     System.out.println();
     }
     }
3.Comparator Program
      // Storing Key/Value pairs itis Integrated with Collections
     import java.util.*;
     class Comp implements Comparator {
     public int compare(Object a,Object b) {
     String astr , bstr;
     astr = (String) a;
     bstr = (String) b;
     return bstr.compareTo(astr);
     }
     }
     public class CompTreeMap {
     public static void main(String[] args) {
     TreeMap tm = new TreeMap(new Comp());
     tm.put("Kannan","Chennai");
     tm.put("Sachin","Bombay");
     tm.put("Elangovan","Bangalore");
     tm.put("Ram","Vellore");
     tm.put("Pathan","Delhi");
     tm.put("Ganguly","Kolkata");
     System.out.println(" Using Comparator Interface Reverse the Contents in the TreeMap : ");
     System.out.println();
     System.out.println("The Size Of the Tree Map is :" +tm.size());
     System.out.println();
     Set tmapset = tm.entrySet();
     Iterator itr = tmapset.iterator();
     while(itr.hasNext()) {
     Map.Entry me = (Map.Entry)itr.next();
     System.out.print(me.getKey() +" ");
     System.out.println(me.getValue());
     }
     System.out.println();
     }
     }
Back to Top
All Rights Reserved : skdotcom technologies