Sunday, August 14, 2011

Get Size of Java LinkedHashMap

package org.best.example;

        /*
      Get Size of Java LinkedHashMap Example
      This Java Example shows how to get the size or nubmer of key value pairs
      stored in LinkedHashMap using size method.
    */
    
    import java.util.LinkedHashMap;
    
    public class GetSizeOfLinkedHashMapExample{
    
    public static void main(String[] args) {
    
    //create LinkedHashMap object
    LinkedHashMap lHashMap = new LinkedHashMap();
    
    /*
      To get the size of LinkedHashMap use
      int size() method of LinkedHashMap class. It returns the number of key value
      pairs stored in LinkedHashMap object.
      */
    System.out.println("Size of LinkedHashMap : " + lHashMap.size());
    
    //add key value pairs to LinkedHashMap using put method
    lHashMap.put("1","One");
    lHashMap.put("2","Two");
    lHashMap.put("3","Three");
    System.out.println("Size of LinkedHashMap after addition : " + lHashMap.size());
    
    //remove one element from LinkedHashMap using remove method
    Object obj = lHashMap.remove("2");
    System.out.println("Size of LinkedHashMap after removal : " + lHashMap.size());
    }
    }
    
    /*
    Output would be
    Size of LinkedHashMap : 0
    Size of LinkedHashMap after addition : 3
    Size of LinkedHashMap after removal : 2
    */

Java Questions - 42

Read the following program:

public class test {
public static void main(String [] args) {
    int x = 3;
    int y = 1;
    if (x = y)
        System.out.println("Not equal");
   else
        System.out.println("Equal");
  }
}

What is the result?
The output is “Equal”
B. The output in “Not Equal”
C. An error at " if (x = y)" causes compilation to fall.
D. The program executes but no output is show on console.
Answer: C
Answer: Transient variable can't be serialize. For example if a variable is declared as transient in a Serializable class and the class is written to an ObjectStream, the value of the variable can't be written to the stream instead when the class is retrieved from the ObjectStream the value of the variable becomes null.

Saturday, August 13, 2011

Get Set view of Keys from Java LinkedHashMap

package org.best.example;

      /*
      Get Set view of Keys from Java LinkedHashMap example
      This Java Example shows how to get a Set of keys contained in LinkedHashMap
      using keySet method of Java LinkedHashMap class.
    */
    
    import java.util.Iterator;
    import java.util.LinkedHashMap;
    import java.util.Set;
    
    public class GetSetViewOfKeysFromLinkedHashMapExample {
    
    public static void main(String[] args) {
    
    //create LinkedHashMap object
    LinkedHashMap lHashMap = new LinkedHashMap();
    
    //add key value pairs to LinkedHashMap
    lHashMap.put("1","One");
    lHashMap.put("2","Two");
    lHashMap.put("3","Three");
    
    /*
      get Set of keys contained in LinkedHashMap using
      Set keySet() method of LinkedHashMap class
      */
    
    Set st = lHashMap.keySet();
    
    System.out.println("Set created from LinkedHashMap Keys contains :");
    //iterate through the Set of keys
    Iterator itr = st.iterator();
    while(itr.hasNext())
    System.out.println(itr.next());
    
    /*
      Please note that resultant Set object is backed by the LinkedHashMap.
      Any key that is removed from Set will also be removed from
      original LinkedHashMap object. The same is not the case with the element
      addition.
      */
    
    //remove 2 from Set
    st.remove("2");
    
    //check if original LinkedHashMap still contains 2
    boolean blnExists = lHashMap.containsKey("2");
    System.out.println("Does LinkedHashMap contain 2 ? " + blnExists);
    }
    }
    
    /*
    Output would be
    Set created from LinkedHashMap Keys contains :
    1
    2
    3
    Does LinkedHashMap contain 2 ? false
    */

Java Questions - 41

What is OOPS?

OOP is the common abbreviation for Object-Oriented Programming.
There are three main principals of oops which are called Polymorphism, Inheritance and Encapsulation.

Friday, August 12, 2011

Java Questions - 40

 Explain the user defined Exceptions?

User defined Exceptions are the separate Exception classes defined by the user for specific purposed. An user defined can created by simply sub-classing it to the Exception class. This allows custom exceptions to be generated (using throw) and caught in the same way as normal exceptions.
Example:

class myCustomException extends Exception {
     / The class simply has to exist to be an exception
}

Check if a particular value exists in Java LinkedHashMap

package org.best.example;

       /*
      Check if a particular value exists in Java LinkedHashMap example
      This Java Example shows how to check if LinkedHashMap object contains a particular
      value using containsValue method of LinkedHashMap class.
    */
    
    import java.util.LinkedHashMap;
    
    public class CheckValueOfLinkedHashMapExample {
    
    public static void main(String[] args) {
    
    //create LinkedHashMap object
    LinkedHashMap lHashMap = new LinkedHashMap();
    
    //add key value pairs to LinkedHashMap
    lHashMap.put("1","One");
    lHashMap.put("2","Two");
    lHashMap.put("3","Three");
    
    /*
      To check whether a particular value exists in LinkedHashMap use
      boolean containsValue(Object key) method of LinkedHashMap class.
      It returns true if the value is mapped to one or more keys in the
      LinkedHashMap otherwise false.
      */
    
    boolean blnExists = lHashMap.containsValue("Two");
    System.out.println("Two exists in LinkedHashMap ? : " + blnExists);
    }
    }
    
    /*
    Output would be
    Two exists in LinkedHashMap ? : true
    */

Thursday, August 11, 2011

Check if a particular key exists in Java LinkedHashMap

package org.best.example;
   
        /*
      Check if a particular key exists in Java LinkedHashMap example
      This Java Example shows how to check if LinkedHashMap object contains a particular
      key using containsKey method of LinkedHashMap class.
    */
    
    import java.util.LinkedHashMap;
    
    public class CheckKeyOfLinkedHashMapExample {
    
    public static void main(String[] args) {
    
    //create LinkedHashMap object
    LinkedHashMap lHashMap = new LinkedHashMap();
    
    //add key value pairs to LinkedHashMap
    lHashMap.put("1","One");
    lHashMap.put("2","Two");
    lHashMap.put("3","Three");
    
    /*
      To check whether a particular key exists in LinkedHashMap use
      boolean containsKey(Object key) method of LinkedHashMap class.
      It returns true if the LinkedHashMap contains mapping for specified key
      otherwise false.
      */
    
    boolean blnExists = lHashMap.containsKey("3");
    System.out.println("3 exists in LinkedHashMap ? : " + blnExists);
    }
    }
    
    /*
    Output would be
    3 exists in LinkedHashMap ? : true
    */