Java 7 - Switch case with non-primitive (String)

 
In last tutorial we have discussed about multi-catch feature in Java 7. Now lets see how Java 7 supports non-primitive datatype in switch case. Until Java 6 we had only switch cases support for primitive datatypes like int, char, short, and byte.  

      Below small example will gives you how switch case works until Java 6 with primitive datatype int.



public class MySwitch {
 public static void main(String[] args) {
  int days = Integer.parseInt(args[0]);
  switch(days){
   case 0: 
    System.out.println("Monday");
    break;
   case 1: 
    System.out.println("Tuesday");
    break;
   case 2: 
    System.out.println("Wednesday");
    break;
   case 3: 
    System.out.println("Thursday");
    break;
   case 4: 
    System.out.println("Friday");
    break;
   case 5: 
    System.out.println("Saturday");
    break;
   case 6: 
    System.out.println("Sunday");
    break;
   default: 
    System.out.println("Sorry Invalid Input!!!");    
  }
 }
}


      In above program we have taken the command line input value and checked for day of week by using switch case. Here we have used primitive datatype int for variable "days".

      Additionally in Java 7 we have got non-primitive datatype String also can be used in the Switch case. Below example will give the same code of Java 7 Switch case with String.



public class MySwitch {
 public static void main(String[] args) {
  String day = args[0];
  
  switch(day.toLowerCase()){
   case "monday": 
    System.out.println("Hai its weeks 1st day...");
    break;
   case "tuesday": 
    System.out.println("Hai its weeks 2nd day...");
    break;
   case "wednesday": 
    System.out.println("Hai its weeks 3rd day...");
    break;
   case "thursday": 
    System.out.println("Hai its weeks 4th day...");
    break;
   case "friday": 
    System.out.println("Hai its weeks 5th day...");
    break;
   case "saturday": 
    System.out.println("Hai its weeks 6th day...");
    break;
   case "sunday": 
    System.out.println("Hai its weeks 7th day...");
    break;
   default: 
    System.out.println("Sorry Invalid Input!!!");    
  }
 }
}



Hope you are clear on new switch case in Java 7 and lets see other Java 7 feature in next tutorial. 






    

Java 7 new Exception Handling using multi-catch

 
Good to hear that in Java 7 has packed with lot of new features and additional features in Exception Handling, Switch case, Collections etc., Today we will discuss about the additional features which we have got from JAVA 7 in Exception handling. 

      Lot of programmers will use single catch() block for handling all exception types by placing the common Exception as like below in Java 6 and earlier versions. 



public class MyClass {
 public static void main(String[] args) {
  try{
   /*
    * You code goes here where multiple
    * exceptions can occur 
    */    
  }catch (Exception e) {
   // Log the exception 
  }
 }
}



      By handling exceptions like above we may lead to lot of issues like handling exceptions efficiently and also its not good practice until Java 6. So in earlier version we used to have multiple catch statements to handle each and every exceptions which may occur in the block as like below. 





public class MyClass {
 public static void main(String[] args) {
  try{
   /*
    * You code goes here where multiple
    * exceptions can occur 
    */    
  }catch (NullPointerException ex) {
   // Log the exception 
  }catch (ClassCastException ex) {
   // Log the exception
  }catch (StackOverflowError err) {
   // Log the error message
  }catch (Exception e) {
   // Log the exception
  }
 }
}


       
      Buy using like this also code goes ugly and lot of huddles in handling each and every Exceptions independently in separate catch blocks. 

     To over come this issue in Java 7 have packed with multi-catch feature in Exception handling and it will be handy to use in a single catch or multiple catch as programmers wish. Lets see small example as how to use multi-catch in Java 7 



public class MyClass {
 public static void main(String[] args) {
  try{
   /*
    * You code goes here where multiple
    * exceptions can occur 
    */    
  }catch (NullPointerException | ClassCastException | StackOverflowError ex) {
   // Log the exception 
  }
 }
}



Hope you are clear and happy that Java 7 has given a good feature for programmers to handle exceptions in a new way. Lets discuss more new features added in Java 7 in our next tutorial. 





How we can get the list of Key from HashMap?

Lets assume in interview you are been asked to list all the values in HashMap. HashMap contains around 1000 values and you have not given the list of keys to get the corresponding values. From HashMap to get the values first you need know the keys and later you can get the list of corresponding values.

            Today we will discuss about how to find the list of Key from HasMap. Before looking into direct program lets see some of the key features of HashMap and different between HashMap and HashTable. HashMap and HashTable will implements Map Interface but there are few difference between both of them where everyone needs to know.

HashMap is not synchronized where as HashTable is synchronized. If one who wants to use HashMap with synchronized then they the synchronize by using Java Collection utility as given below.
Since HashTable is synchronized by default it will slower than HashMap and performance wise HashMap will be better.
HashTable is defined in earlier version before Java Collections introduced.
HashMap will allow null key and value and where as in HashTable you cant place null key or value.
Iterator in HashMap is a fail-fast if any one tries to modify or remove the element from the HashMap and where as Enumeration in HashTable is not such. 
In HashMap value or key order will not be guarantee that will be same as inserted.

   

public class MyKeyList {
       
       public static void main(String[] args) {
               HashMap<String, String> hm = new MyKeyList().getHashMap();
                    
               Set<String> set = (Set<String>)hm.keySet();
               Iterator<String> itr = set.iterator();
               while(itr.hasNext()){
                       System.out.println(itr.next());
               }
               
               // Other way just by for loop as 
               for(Object key : hm.keySet()){
   System.out.println(hm.get(key));
  }

       }
       
       private HashMap<String, String> getHashMap(){
               HashMap<String, String> hm = new HashMap<String, String>();
               hm.put("one", "java");
               hm.put("four", ".NET");
               hm.put("two", "C");
               hm.put("five", "PHP");
               hm.put("three", "C++");
               return hm;
       }        
}


      In above program we have saved 5 values in HashMap and its been returned to main method and we have used Set and Iterator to get keys from the HashMap.
      Here need to remember about the function called ketSet() used to get the set of Keys from HashMap.



OUTPUT:



one
two
five
three
four



Hope you are clear now how to get list of Key from HashMap. You have any comments please drop it down.







How Java solves the problem of platform independent ?

In this tutorial we will discuss small information on how Java solves the problem of platform independent. Before seeing the solution as how its working we need to know answers for few questions like,

1. What is Java bytecode ?
2. What is machine code ?
3. What is JVM ?

Once we know about all these questions we can understand easily as how Java sloves this problem.

 

What is Java bytecode ?
 

      Each Java program which we write will be converted to bytecode by Java Compiler. From .java (Java program) to .class (Class file) file which contains byte codes. There are around 200 byte codes instructions which are used to covert any type of complex Java program to bytecodes files. Each byte codes instructions are of 8 bit which is 1 byte, hence its called Java bytecode.

What is Machine code?
 

     The lowest-level programming language is Machine languages and only the languages can understood by computers. Machine languages are impossible to understand by humans. Programmers will use high-level programming languages like (C, C++, Java, etc.,) are translated into machine language by a compiler. Every Operating System has its own unique machine language. Each high-level programs must be compiled to run on different types of computers.
For Example .exe file format is a machine language files where only Windows OS can understand and those files can't be executed in Mac or Linux etc.,


What id JVM?
 

     JVM is nothing but Java Virtual Machine. As the name indicates Virtual its not a real machine. Bytecodes compiled by compiler can be understand by any JVM and JVM used to convert the bytecode files to each machine code dependent files and executes the program. Where JVM are platform dependent and used to translate the bytecode into the machine language for a particular computer, and actually execute the corresponding machine-language instructions as well.

     Finally JVM are platform dependent and used to convert the byte code into corresponding platform machine languages. By this way Java solves the problem of platform independent.

Hope you are clear on how Java solves the platform independent problem. 





Object sorting using Comparator and Comparable Interface in Java

Today we will discuss about how to sort Object using Comparator and Comparable interface in Java. 

       How to sort Object using Comparator and Comparable Interface in Java is a frequent question in most of the Core Java interview questions. It can be just theory question or even some interviewer may ask to write sample code for both. In that case one has to be prepared for both to explain the different and importance of both Interface in Java and also to write a sample code. 

       Lets see some important different between these two Interface's in Java. 

  • Comparator Interface is from java.util package and Comparable Interface is from java.lang package.
  • For using Comparator Interface we need to import java.util.Comparator, where as Comparable is by default. 
  • If we implement Comparator Interface then we need to implement method called public int compare(Object obj1, Object obj2) and for Comparable Interface we need to implement method called public int compareTo(Objectobj)
  • If we see Comparator Interface it will be used to Compare 2 objects different Objects of a class and where Comparable Interface will be used to Compare same class local member variables with the Object provided to CompareTo method. 

          
     Example:

       Below example Employee Class which will implement both Comparator and Comparable Interface and also has the method implementation for compare() and compareTo() methods. 
       compare() method has 2 input parameters of Employee class object and used to sort based on Employee Name (empName). 
       comapreTo() method has single Employee class object and sorted based Employee Address (empAddress)     


Employee.java

public class Employee implements Comparator<Employee>, Comparable<Employee> {

 private String empName;
 private String empAddress;

 public Employee() {
 }

 public Employee(String empName, String empAddress) {
  this.empAddress = empAddress;
  this.empName = empName;
 }

 public String getEmpName() {
  return empName;
 }

 public void setEmpName(String empName) {
  this.empName = empName;
 }

 public String getEmpAddress() {
  return empAddress;
 }

 public void setEmpAddress(String empAddress) {
  this.empAddress = empAddress;
 }

 public int compare(Employee o1, Employee o2) {
  String o1Name = o1.getEmpName();
  String o2Name = o2.getEmpName();
  if (o1Name.compareTo(o2Name) > 0) {
   return 1;
  } else if (o1Name.compareTo(o2Name) < 0) {
   return -1;
  } else {
   return 0;
  }
 }

 public int compareTo(Employee obj) {
  return this.empAddress.compareTo(obj.empAddress);
 }
}

       

CompratorComparableTest.java

public class CompratorComparableTest {
 public static void main(String[] args) {
  
  List<Employee> arr = new ArrayList<Employee>();
  arr.add(new Employee("jobs", "delhi"));
  arr.add(new Employee("mike", "bangalore"));
  arr.add(new Employee("vera", "madras"));
  arr.add(new Employee("raja", "chennai"));
  arr.add(new Employee("mani", "rorkee"));
  arr.add(new Employee("khan", "mumbai"));
  
  // Object sort using comprator
  Collections.sort(arr, new Employee());  
  Iterator<Employee> itr = arr.iterator();
  System.out.println("Object sort using comparator - sort by Name");
  while (itr.hasNext()) {
   Employee emp = (Employee) itr.next();
   System.out.println(emp.getEmpName()+" \t\t "+emp.getEmpAddress());
  }
  
  System.out.println("\n\n");
  
  //Object sort using comprable
  Collections.sort(arr);
  Iterator<Employee> itr1 = arr.iterator();
  System.out.println("Object sort using comprable - sort by Address");
  while (itr1.hasNext()) {
   Employee empobj = (Employee) itr1.next();
   System.out.println(empobj.getEmpName()+" \t\t "+empobj.getEmpAddress());
  }
 }
}




OUTPUT:


Object sort using comparator - sort by Name
jobs    delhi
khan    mumbai
mani    rorkee
mike    bangalore
raja    chennai
vera    madras



Object sort using comprable - sort by Address
mike    bangalore
raja    chennai
jobs    delhi
vera    madras
khan    mumbai
mani    rorkee


Hope you are clear on using Comparator and Comparable Interface in Java. Please drop your comments.  




 

How to create fully Singleton design pattern class in Java

Today we will discuss about how to create fully Singleton class in Java. Before jumping to creation of Singleton class, lets list down some of the key features and usage of creating Singleton class in Java. 

What is Singleton design pattern class?

         In simple words Singleton class is nothing but having a single object or Instance of a class for entire lifetime of the application. 

       Singleton pattern is a common pattern we use in our day today code and projects in various places like reading a property file values, DB Connections etc.Restricting multiple Object or Instance creation on Class. Main property of singleton class is restricting the class Instance creation from outside Singleton class. This can be achieved by keeping private constructors. 

       Even by keeping private constructors other programmers can break singleton pattern and they can create instance outside class by other Java features like clone() and serialization. To avoid these loop hols we need to @Override clone() and readResolve() methods in our Singleton class.

       We can achieve this in 2 ways like Lazy initialization and Eager initialization in the class. Below are the small example which will explain about the Fully Singleton implementation by using Lazy initialization.


import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.Random;

import javax.management.InstanceAlreadyExistsException;

class MySingleton implements Serializable  {
    
 private static MySingleton obj = null;
    private int val;
    
    private MySingleton() throws InstanceAlreadyExistsException{
     if(obj != null){
      System.out.println("\nObject already created....");
      throw new InstanceAlreadyExistsException();
     }
 
     System.out.println("Inside Constructor..");
        val = new Random().nextInt();
    }

    public static MySingleton getInstance() throws InstanceAlreadyExistsException{
        if(obj == null){
            synchronized (MySingleton.class) {
                if(obj == null){
                    obj = new MySingleton();
                }
            }
        }
        return obj;
    }
    public int getVal(){
        return val;
    }
    public void setVal(int val){
     this.val = val;
    }
    
    @Override
    protected MySingleton clone() throws CloneNotSupportedException {
     // If user tries to clone the object then we are sending same class object
        try {
   return MySingleton.getInstance();
  } catch (InstanceAlreadyExistsException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return null; 
    }
    
    private Object readResolve() throws ObjectStreamException, InstanceAlreadyExistsException {
     // We are blocking deserilizing object and sending same class object
      return MySingleton.getInstance();
  }
}


Above MySingleton class contains private constructor, getter, setter, @Override clone method, readResolve method and static method to get the class instance. Apart from that we have class member variables like MySingleton (obj) and val. In constructor just we have assigned some random value to val just for identifying multiple instance has created are we are using same instance throughout the application and next we are throwing exception called InstanceAlreadyExistsException if already class instance created. Here we are blocking other user to create instance using java.lang.reflect.Constructor class to satisfy fully singleton pattern.

          In static method we are using double to identify class has already initialized or not. If already initialized then method we will return Object (obj) else condition will go inside synchronized block. Here we are using synchronized block to avoid multiple object creation under multi-threading.


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;

public class SingletonPattern {

    public static void main(String[] args)  {
     try{
         MySingleton obj1 = MySingleton.getInstance();
         MySingleton obj2 = obj1.clone();
        
         ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\\singleton.ser")); 
         oos.writeObject(obj1); 
         oos.close(); 
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("C:\\singleton.ser")); 
         MySingleton obj3 = (MySingleton) ois.readObject(); 
         ois.close(); 
        
         System.out.println("First Object      :: "+obj1.getVal());
         System.out.println("Clone Object      :: "+obj2.getVal());
         System.out.println("Serialized Object :: "+obj3.getVal()+"\n\n");
         
         // Changing value in Object 3
         obj3.setVal(100);
         
         System.out.println("First Object      :: "+obj1.getVal());
         System.out.println("Clone Object      :: "+obj2.getVal());
         System.out.println("Serialized Object :: "+obj3.getVal()+"\n\n");
         
         // Changing value in Object 3
         obj2.setVal(1234);
         
         System.out.println("First Object      :: "+obj1.getVal());
         System.out.println("Clone Object      :: "+obj2.getVal());
         System.out.println("Serialized Object :: "+obj3.getVal());
         
         Constructor constructor = MySingleton.class.getDeclaredConstructor();
         constructor.setAccessible(true);
         MySingleton newInstance = (MySingleton) constructor.newInstance();
         
         System.out.println("NEW INSTANCE : "+newInstance.getVal());

     }catch (Exception e) {
   System.out.println(e.getCause());
  }
    }
}


In above class we created the Object (obj1) by calling static getInstance() method. 
         
         Next we have cloned the same Object (obj1) to other Object (obj2), since we have @Overrided clone() method in our first class it will return same Instance which have initialized at first time and we are blocking the cloning the Object here.

         Next we have serialized the Object (Obj1) into a file and then again we have deserialized the Object to (ob3). 

         Next we are changing the "val" value of Object (obj3) where we can see value changed in all 3 Objects in the Output and we can confirm that user can't create new Object through serializing. 

         Next we are changing the "val" value of Object (obj2) where same way value has changed in all 3 Objects in the Output and we can confirm that even by cloning user will same Object instead of new Object.

         Last we have testing whether are able to create instance using java.lang.reflect.Constructor class. In that case we will get exception called InstanceAlreadyExistsException and new instance can't be created. 

OUTPUT: 


Inside Constructor..
First Object      :: 739385266
Clone Object      :: 739385266
Serialized Object :: 739385266


First Object      :: 100
Clone Object      :: 100
Serialized Object :: 100


First Object      :: 1234
Clone Object      :: 1234
Serialized Object :: 1234

Object already created....
javax.management.InstanceAlreadyExistsException



Hope you are clear now how to create fully Singleton class. Please drop your comments below.



 

Fetch unique object values from ArrayList without looping

Today we will discuss on simple Java Collection interview question as how to fetch unique object values from ArrayList without looping through ArrayList. We can achieve by @Overriding hascode() and equals() method in our class.

Example:
 
       Lets assume we have box full of pencils with various colors like (red, green, blue, yellow etc.,) around 2000 pencils. We need to find total no. of colors in the box. 


       Lets assume box is the ArrayList and Pencil is the class with member variable called color. Below sample code is the one of the way we can fetch unique color of pencils in the ArrayList without looping.




 
 
 
public class Pencil{
 private String color;
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public Pencil(String color){
        this.color = color;
    }
    @Override
    public int hashCode() {
     return 0;
    }     
    @Override
    public boolean equals(Object obj) {
        return ((Pencil)obj).color == this.color;
    }
}


       Above Pencil class contains color member variable, constructor, getter and setter method. Along with these methods we do have @Override method implementation for hascode() and equals() which will be used to find the color of each object stored in ArrayList and while storing in HashSet will elimate duplicate value from below code.



 public class PencilColors {
 public static void main(String[] args) {
  ArrayList<Pencil> box = new ArrayList<Pencil>();
        box.add(new Pencil("red"));
        box.add(new Pencil("green"));
        box.add(new Pencil("blue"));
        box.add(new Pencil("red"));
        box.add(new Pencil("blue"));
        Set<Pencil> list = new HashSet<Pencil>(box);
        Iterator<Pencil> itr = list.iterator();
        while(itr.hasNext()){
            Pencil pencil = itr.next();
            System.out.println(pencil.getColor());
        }
    }
}



OUTPUT:

blue
green
red


Hope you are clear now how to fetch unique values from ArrayList without looping just by using Java Collections. Please drop your comments below.