Beruflich Dokumente
Kultur Dokumente
Result : All 11 objects are added and not able to fetch the result.
Output :: 11
@Override
public boolean equals(Object obj) {
System.out.println("equals() called to check :: " + this.hashCode() + " vs " + obj.hashCode());
if (this == obj) {
System.out.println("First : true");
return true;
}
if (obj == null) {
System.out.println("Second : false");
return false;
}
if (getClass() != obj.getClass()) {
System.out.println("Third : false");
return false;
}
Pen other = (Pen) obj;
if (color == null) {
if (other.color != null) {
System.out.println("Fourth : false");
return false;
}
} else if (!color.equals(other.color)) {
System.out.println("Fifth : false");
return false;
}
System.out.println("Sixth : true");
return true;
}
Result : All 11 objects are added in the set means there is not check of equals method. Even the
control never enters in the equals method. Because I have given some output for each
condition and even if it does not enter in any condition it should result last output. But there is
no output text appear in the output panel.
Output :: 11
Situation : 3 : Override only hashCode() method normally.
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((color == null) ? 0 : color.hashCode());
return result;
}
Output : 11
Situation : 4 : Override both hashCode() and equals() by taking "color" field of Pen class.
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((color == null) ? 0 : color.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
System.out.println("equals() called to check :: " + this.hashCode() + " vs " + obj.hashCode());
if (this == obj) {
System.out.println("First : true");
return true;
}
if (obj == null) {
System.out.println("Second : false");
return false;
}
if (getClass() != obj.getClass()) {
System.out.println("Third : false");
return false;
}
Pen other = (Pen) obj;
if (color == null) {
if (other.color != null) {
System.out.println("Fourth : false");
return false;
}
} else if (!color.equals(other.color)) {
System.out.println("Fifth : false");
return false;
}
System.out.println("Sixth : true");
return true;
}
Result : equal() return eight times true means only 3 are added in the HashSet. In this case
equals() is called 8 times.
Situation : 5 : override both hashCode() (with a fixed return value) and equals() as it is.
@Override
public int hashCode() {
int result = 1;
return result;
}
Result : Final result as is before there is no impact of result. But call of equals() is increased i.e.
16 times.
Output :
Result : Result is not fixed. Output is being vary each time. it is giving different output every
time.
Output :
1. First Time
2. Second Time ::
3. Third Time
While changing the data structure as TreeSet and let us see what happened.
Situation 1 : If use TreeSet need to implements Comparable and override compareTo method.
and hashCode() and equals() left as it is. even I removed entire equals() method.
Result : Result is always 3 and equals() method not called even 1 time. Even I removed equals()
method there is no impact in the output.
Output : 3
Situation : 2 : If I removed both hashCode() and equals() and there is only Comparable and
compareTo() method.
Result : no impact. the output is still same. if you are giving compareTo() implementation and
either you give or not for overriden method equals() and hashCode() it is not called even.
Output : 3
1. Java has main method ,where is that main method exists in web application.
2. How request flows in Spring MVC.What is servlet in Spring MVC and what it does.
3. We need to monitor the time taken by each dao method to fetch data from
database/server.
We don’t want to add logger or any timestamp statement in each method.
How will you achieve it efficiently.
4. We have a collection of different color markers. Write code to fetch all unique colored
markers.
5. What is Hash code and equals.
6. Sample code was there.
Class A{
doJob(String task){}
}
Class B extends A
{
doJob(String task){}
p.s.v main(){
new B().doJob()// will it compile?
// is it overloading or overriding ?
}