Wednesday, August 31, 2016

HashMap internals

Write operation in Hashmap

         API call à hashmap.put(KEY,VALUE);
         1.  When you call this function, KEY (object) hashcode function gets called.
         2.  return value of the hashcode() is integer.
         3.  This value is divided by max_bucket value    
         4.   key,value and hashcode are stored respective buckets new entry        

NOTE : So far, we haven’t discussed about the equals function.





Read opearion in Hashmap

API call à hashmap.get(KEY);
         1.  When you call this function, KEY (object) hashcode function gets called.
         2.  return value of the hashcode() is integer.
         3. from the hashcode, it finds the bucket and traverse to the list to find the hashcode
         4. Once it matches with hashcode, it checks with object equality using equals() method defined in KEY class ( from this point, you can infer another point.
          a) if objects are equal, then hashcode must be same. So that it fall in same bucket
          b) if hashcode is same, it doesn’t mean that objects are equals  )
  

Tuesday, August 30, 2016

Configure Spark on windows , some error and solution


Step 1 : Download and install latest version of scala
Step 2 : Download and install latest version of sbt
Step 3 : download any spark version and extract using linux environment
Step 4 : Go to the root folder of the spark  and run "sbt package" command
        This will download few jar file , it may take some time.
       add proxy in sbtconfig.txt (sbt\conf\sbtconfig.txt) in case you are behind proxy

Step 5: run spark-shell
  Error :
 C:\Program Files (x86)\spark-2.0.0-bin-hadoop2.7\bin>spark-shell
'Files' is not recognized as an internal or external command,
operable program or batch file.
Failed to find Spark jars directory.
You need to build Spark before running this program.

Solution : move your spark folder to C:\













You may end up below error while running Spark in local mode
16/08/30 17:40:29 ERROR RetryingBlockFetcher: Exception while beginning fetch of 1 outstanding blocks
java.io.IOException: Failed to connect to /10.91.152.141:62303
        at org.apache.spark.network.client.TransportClientFactory.createClient(TransportClientFactory.java:228)

Solution for above error
     Spark/bin > spark-shell --master local


Error :
  ERROR Shell: Failed to locate the winutils binary in the hadoop binary path
java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries.

Solution :
     Add new environmental variable HADOOP_HOME and value should be path of the winutils.exe





Monday, August 29, 2016

what is Immutable and benefit of Immutable

What is immutable class
         Value of the member variable can’t change once its set

What is the benefit of having immutable?
     As immutable class is read only, it can be used in multi-threading environment without worrying about race condition
        
How to create a class as immutable
   Goal : Create a class, if you have created object for that class, then value should be changed
   Let’s take a class employee
Step 1:  create employee class

  class employee{  
    private int emp_id;  
    private String emp_name;  
    private int emp_age;  
    }  

Problem :  Anyone can declare public function through which can change the value of the variable.
Solution : Declare those variable as final, So that only constructor can assign the value to  the variable

Step 2 : Declare member variable as final

 class employee{  
    private final int emp_id;  
    private final String emp_name;  
    private final int emp_age;  
    public employee (int ID,String NAME , int age){  
        emp_id = ID;  
        emp_name = NAME;  
        emp_age = age;   
     }  
    }  

Remember, if you are passing any object as an input param, then that needs to be deep copied.
Problem : How to retrieve the value of the class
Solution:  Add getter method  , scope is public

Step 3 : Add getter method to retrieve the value

 public int getID(){  
   return emp_id;  
 }  
 public String getName(){  
     return emp_name;  
 }   
        

Step 4 : declare class as final, So that it can’t be inherited

 public final class employee{  
 …  
 …  
 }  

What are the immutable classes available in java ?

         String   class is the best example of the immutable 

Wednesday, August 24, 2016

Hashcode and Equals method


Both methods are declared in Object.java (which is parent for all the java classes)

Below snapshot taken from Object.java file



 public boolean equals(Object o) {  
     return this == o;  
   }  
 public int hashCode() {  
     int lockWord = shadow$_monitor_;  
     final int lockWordMask = 0xC0000000; // Top 2 bits.  
     final int lockWordStateHash = 0x80000000; // Top 2 bits are value 2 (kStateHash).  
     if ((lockWord & lockWordMask) == lockWordStateHash) {  
       return lockWord & ~lockWordMask;  
     }  
     return System.identityHashCode(this);  
   }  


Equals method : To check the equality of the two object
    from the above code , if you are not overriding the equals method, then it will check memory address rather than the object . 

  Assume you have a employee class where emp_id is unique , then you need to override the eqquls like below 


 @Override
 public boolean equals(Object o) {  
     return this.emp_id == o.emp_id;  
   }  



Hashcode method : 
       hashcode method will comes in to the picture , if you are using this class's object  as a key in hashmap.
you no need to override this method if you are not using this class as a key.

Nowadays, modern editor will implement hashcode() for you to get the decent hashcode value.


Ok, now we have seen hashcode and equals method separately.. 
  Now ,Question is , any contract between this two method ...

Contract between the two methods Yes ,  in case two object is equal , then hashcode value should be same.
         if hashcode of the two object is same , then object may or may not be equal.

Well, to understand the above statement , we need to know the internal implementation of the hashmap and how read and write happens.

will talk about the those topic soon. stay tuned.   
         

Tuesday, August 23, 2016

Solution for error : java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri


Error :
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder;
javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:626)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


Pom.xml file

<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.2</version>
</dependency>

Solution:
there is a conflict , you need to remove javax.ws.rs dependency .. anyway com.sun.jersy have the same classes to do the task.

Saturday, August 20, 2016

Singleton pattern - step by step guide

                                              
Goal:  to have single object as long as app exists

How to achieve that:

     Step 1:  have a private constructor of the class
              This helps, you can’t create object of the class from outside

 Class singleton{   
     Private singleton(){   
     }   
   }  

Ok, then How to create an object

Step 2:  Have a static public function
        Responsibility of this function is to create an object
     Why public  scope :  So that, this function can be called from outside
     Why static  : this function can be called without creating object of it .

 Class singleton{  
      Private static singleton singletonObj;  
       Private singleton(){  
       }  
       Public static singleton getInstance(){  
          If(null == singletonObj )  
            singletonObj = new singleton();  
        return singletonObj;  
       }   
   }  

We have now created singletonObj as single instance.
    Is my job over ?    this implementation is good as long as you are working with single threaded application.  But, the modern applications are using multithreaded.


Step 3:  Use synchronized block
     
 Public static singleton getInstance(){   
     Synchronized(singleton.class) {   
      If(null == singletonObj )   
       singletonObj = new singleton();   
      }   
     return singletonObj;   
     }   

Well, we have achieved it.  
  Now,  Assume singletonObj created, Thread t1 and t2 going to call this method to get the singletonObj.
   Thread t1 calling this method, as soon as it enters in to the function, lock is acquired because of the synchronized block. And t1 is not exiting this function.
    Now , t2 calling this method , as already lock is held by Thread t1, Thread t2 will wait to release the lock.
     This is performance issue .


Step 4 :  Add null check

Public static singleton getInstance(){   
      If(null == singletonObj ) {   
     Synchronized(singleton.class) {   
      If(null == singletonObj )   
       singletonObj = new singleton();   
      }   
     }   
     return singletonObj;   
     }   

If object is created, then it will call the return method directly.

Another optimization technique is, Using different lock

 Class singleton{  
      Private static singleton singletonObj;  
      Private Object lockObj = new Object();  
       Private singleton(){  
       }  
      Public static singleton getInstance(){  
          If(null == singletonObj ) {  
           Synchronized(lockObj) {  
              If(null == singletonObj )  
             singletonObj = new singleton();  
         }  
       }  
        return singletonObj;  
       }  
   }  

In case you have different function holding singleton class lock, then without any problem you can access this function.
   But still the previous implementation holds good, because synchronized function gets executed only once (ie till creation of the object ) whereas in new implementation we have created object, that will be alive till the app exists.