Sunday 28 February 2016

Understanding hashCode and equals contract

When it comes to storing objects in a hashMap, we must follow the contract between hashCode() and equals(). The contract says:

  1. If two objects are equal, then they must have the same hash code.
  2. If two objects have the same hash code, they may or may not be equal.
or in other words
If equal, then same hash codes too.
Same hash codes no guarantee of being equal.

Lets see the scenarios what happens when we deal with object in hashMap:
import java.util.HashMap;

// Scenario 1 : Neither of  equals() or hashCode() is overridden 
import java.util.*;
 class Apple {
private String color;
public Apple(String color) {
this.color = color;
}
  public static void main(String[] args) {
Apple a1 = new Apple("green");
Apple a2 = new Apple("red");
//hashMap stores apple type and its quantity
HashMap<Apple, Integer> m = new HashMap<Apple, Integer>();
m.put(a1, 10);
m.put(a2, 20);
System.out.println(m.get(new Apple("green")));
}
}

The out put is simply "null" (of course which is not expected)
The reason is when we are calling m.get(new Apple("green")), the hash created by hashCode() would be different as its will hold a different memory location. So it will not be available in the hash table bucket.

// Scenario 2 : Overriding equals() 
import java.util.*;
 class Apple {
private String color;
public Apple(String color) {
this.color = color;
}
      //overriding equals
public boolean equals(Object obj) {
if(obj==null) return false;
if (!(obj instanceof Apple))
return false;
if (obj == this)
return true;
return this.color.equals(((Apple) obj).color);
}

  public static void main(String[] args) {
Apple a1 = new Apple("green");
Apple a2 = new Apple("red");
//hashMap stores apple type and its quantity
HashMap<Apple, Integer> m = new HashMap<Apple, Integer>();
m.put(a1, 10);
m.put(a2, 20);
System.out.println(m.get(new Apple("green")));
}
}

Still the out put is  "null" (again which is not expected)
Reason: Here we broke the contract of hashCode() and equals(). We must override hashCode() when we override equals(). Because the default behavior of equals() is to compare memory of the objects. When we are overriding the behavior of equals this will change the default behaviour..which leads to breaking the contract between hashCode() and equals(){If two objects are equal, then they must have the same hash code.}

// Scenario 3 : Overriding both equals() and hashCode() {Correct scenario}

import java.util.*;
 class Apple {
private String color;
public Apple(String color) {
this.color = color;
}
      //overriding equals
public boolean equals(Object obj) {
if(obj==null) return false;
if (!(obj instanceof Apple))
return false;
if (obj == this)
return true;
return this.color.equals(((Apple) obj).color);
}
     //overriding hashCode
public int hashCode(){
return this.color.hashCode();
}

  public static void main(String[] args) {
Apple a1 = new Apple("green");
Apple a2 = new Apple("red");
//hashMap stores apple type and its quantity
HashMap<Apple, Integer> m = new HashMap<Apple, Integer>();
m.put(a1, 10);
m.put(a2, 20);
System.out.println(m.get(new Apple("green")));
}
}

Now hope you hape a better understanding why we should override hashCode() and equals() while dealing with hashMap and objects.
References:

Thursday 11 February 2016

Why Should you buy a DSLR

This is the first question should come in your mind when you are thinking to buy a DSLR.
Let's play a Devil's advocate to reconsidering your decision.
Why do you really need a DSLR? People will tell you about the absolute advantages of a DSLR but you don't buy a Harley Davidson just because its features and specs are awesome(I know some idiots do it, I'm the one who is planning to do the same).
Most first time user/photography enthusiasts need a good camera for capturing family and general travel photos. They don't intend to freeze themselves in mountains trying to get that perfect night shot, or spend 50% of their honeymoon time trying to setup tripods (which btw, is a bad idea).The

DSLRs are awesome only if :
  • Passionate about photography
  • Need specific kind of pictures
  • Intend to devote some part of your life to do something which brings happiness to you
  • Have a one who considers you as an object when hand over your DSLR and brings best out of you.
Now lets see some of the comparative advantages I've collected to have a DSLR

  • Bigger sensor - Sensor is where all the data shot by a camera gets recorded. Imagine rain drop falling from sky as light. Keeping a bucket and keeping a glass outside, the bucket will gather more water from the same source in same time. Big sensor = Big bucket. Is that better? Yes, if you are willing to exploit that data (aka process pictures in an editing software).


In the picture above you can see how it differentiates one object from the others.

  • Night photography :If you have a DSLR you could be the king or queen of photography  where others have a normal digital camera, There you would realize it's worth having one. DSLRs are the Rocky Balboas of night photography.

  • Depth of Field : One of the things I love about my DSLR is the versatility that it gives in many areas, especially depth of field. I guess this is really an extension of it’s manual controls and ability to use a variety of lenses but a DSLR can give you depth of field that puts everything from foreground to background in focus through to nice blurry backgrounds.

There are plenty of advantages you will have when you have one have one in your hand, specially the one when people will come around and give an exclamation that you are holding a DSLR :P.

Well, well well enough for advantages, now lets have a small note on the disadvantages you could have:
Expensive : Obviously difficult to afford for the people like me :P
Weight : I can carry it easily but some of you might feel a bit heavy.
Ease of use : Initially have to struggle to make angle and take proper shots.

Have a nice photography with your digital single-lens reflex....Saifu :)