Sie sind auf Seite 1von 8

Situation : 1.

When there is no equals and hashCode overridden,

Result : All 11 objects are added and not able to fetch the result.

Output :: 11

Situation : 2 : Override only equals() method.

@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;
}

Result : Still result is same.

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.

Output :: The Desired Result.


equals() called to check :: 82064 vs 82064
Sixth : true
equals() called to check :: 82064 vs 82064
Sixth : true
equals() called to check :: 2073753 vs 2073753
Sixth : true
equals() called to check :: 82064 vs 82064
Sixth : true
equals() called to check :: 82064 vs 82064
Sixth : true
equals() called to check :: 82064 vs 82064
Sixth : true
equals() called to check :: 69066498 vs 69066498
Sixth : true
equals() called to check :: 69066498 vs 69066498
Sixth : true
3

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 :

equals() called to check :: 1 vs 1


Fifth : false
equals() called to check :: 1 vs 1
Fifth : false
equals() called to check :: 1 vs 1
Fifth : false
equals() called to check :: 1 vs 1
Sixth : true
equals() called to check :: 1 vs 1
Sixth : true
equals() called to check :: 1 vs 1
Fifth : false
equals() called to check :: 1 vs 1
Sixth : true
equals() called to check :: 1 vs 1
Sixth : true
equals() called to check :: 1 vs 1
Sixth : true
equals() called to check :: 1 vs 1
Sixth : true
equals() called to check :: 1 vs 1
Fifth : false
equals() called to check :: 1 vs 1
Fifth : false
equals() called to check :: 1 vs 1
Sixth : true
equals() called to check :: 1 vs 1
Fifth : false
equals() called to check :: 1 vs 1
Fifth : false
equals() called to check :: 1 vs 1
Sixth : true
3

Situation : 6 : equals() left as it is implemented. but hashCode() changed as used


Math.random() then
@Override
public int hashCode() {
int result = (int)Math.round(Math.random());
return result;
}

Result : Result is not fixed. Output is being vary each time. it is giving different output every
time.

Output :

1. First Time

equals() called to check :: 0 vs 1


Fifth : false
equals() called to check :: 1 vs 1
Fifth : false
equals() called to check :: 0 vs 0
Sixth : true
equals() called to check :: 1 vs 0
Fifth : false
equals() called to check :: 1 vs 1
Sixth : true
equals() called to check :: 1 vs 0
Fifth : false
equals() called to check :: 1 vs 1
Sixth : true
equals() called to check :: 0 vs 1
Sixth : true
equals() called to check :: 1 vs 0
Sixth : true
equals() called to check :: 0 vs 1
Sixth : true
equals() called to check :: 1 vs 1
Sixth : true
4

2. Second Time ::

equals() called to check :: 1 vs 0


Fifth : false
equals() called to check :: 0 vs 0
Fifth : false
equals() called to check :: 1 vs 0
Fifth : false
equals() called to check :: 0 vs 1
Sixth : true
equals() called to check :: 0 vs 1
Fifth : false
equals() called to check :: 1 vs 1
Sixth : true
equals() called to check :: 1 vs 0
Sixth : true
equals() called to check :: 0 vs 0
Sixth : true
equals() called to check :: 0 vs 1
Fifth : false
equals() called to check :: 0 vs 0
Fifth : false
equals() called to check :: 1 vs 0
Sixth : true
equals() called to check :: 0 vs 0
Fifth : false
equals() called to check :: 0 vs 0
Fifth : false
equals() called to check :: 1 vs 0
Sixth : true
5

3. Third Time

equals() called to check :: 1 vs 1


Fifth : false
equals() called to check :: 1 vs 1
Fifth : false
equals() called to check :: 0 vs 1
Sixth : true
equals() called to check :: 0 vs 1
Fifth : false
equals() called to check :: 0 vs 0
Fifth : false
equals() called to check :: 0 vs 0
Fifth : false
equals() called to check :: 0 vs 1
Sixth : true
equals() called to check :: 0 vs 1
Sixth : true
equals() called to check :: 1 vs 1
Sixth : true
equals() called to check :: 0 vs 0
Sixth : true
equals() called to check :: 0 vs 1
Fifth : false
equals() called to check :: 0 vs 1
Fifth : false
6
etc.

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.

public int compareTo(Object object) {


if(object instanceof Pen)
{
Pen pp = (Pen)object;
return (this.color.compareTo(pp.color));
}
else
return -1;
}

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 ?
}

10) What is Stream api ,Hash Map changes (java8).


11) Expalin StringBuffer and StringBuilder comparisons.
12) Contract between hashcode and equals method.
13) Expalin ORM features.
14) Jpa,HQL,Named queries.
15) Decorator ,Proxy Pattern.
16) What is Session Management in Rest
17) Explain Streams Lamda in Java 8
18) Why we use Functional programing
19) Coding in java 8.
20) What is abstraction and encapsulation?
21) What is an abstract class? When would you use Abstract class over interface?
22) What is polymorphism?
23) What is a singleton? Write code for a singleton class.

Das könnte Ihnen auch gefallen