7) Suppose there is Student class like

class student {
int age;
string name;
}

We want to store these objects in a HashMap. Do we need to
override any methods in Student class? If any which ones
and why? what if i just override equals or just hashcode?
what will be the results in both the cases?

Answers were Sorted based on User's Feedback



7) Suppose there is Student class like class student { int age; string name; } We want to s..

Answer / manoranjan

Logically no need to override any method but problem with
this design will be that we can not find the objects stored
in the hashmap. So to overcome this problem we need to
override equlas() and hashcode() mathod of Object
class.According to Sun java guide line if equals() method is
teeling that two objects are equal then their hashcode must
be same.But reverse is not true.

Is This Answer Correct ?    10 Yes 0 No

7) Suppose there is Student class like class student { int age; string name; } We want to s..

Answer / lucky singh

It is a good practice to override equals() and hashcode()
methods if you are dealing with collections. Both of the
functions are used while searching for a particular object
in a hashmap. In general, equals() method is used to
determine if two objects are meaningfully equal or not. And
hashcode() method is used to determine if two objects are
meaningfully unequal or not.

Is This Answer Correct ?    2 Yes 0 No

7) Suppose there is Student class like class student { int age; string name; } We want to s..

Answer / krishna

There is no need to override equals() and hashCode() methods. But if we want to eliminate duplicate objects in the HashMap, you should override both the methods. Either one is not enough.

Is This Answer Correct ?    0 Yes 0 No

7) Suppose there is Student class like class student { int age; string name; } We want to s..

Answer / punch

Their is NO need to override any methods of student class,
bcoz in the ket of hashmap we are using String and equal
method and hashcode method of String class is already
overrided . So NO need to override any methods and take help
of String class of the same.

Is This Answer Correct ?    1 Yes 7 No

Post New Answer

More Core Java Interview Questions

There can be a abstract class without abstract methods, but what is the need to declare a class abstract if it doesn't contain abstract methods?

5 Answers   HCL,


In Java why we write public static void main(String args[]) why not main()?

46 Answers   Aptech, GE Healthcare, Infosys, Microsoft, New Horizon, Practical Viva Questions, TCS, Wipro,


What is the null?

0 Answers  


What is difference between calling start() and run() method of thread?

0 Answers  


What is a method in coding?

0 Answers  






What is an exception in java?

0 Answers  


Is delete, next, main, exit or null keyword in java?

1 Answers  


Is space a string in java?

0 Answers  


What is the difference between a local variable and an instance variable?

0 Answers  


What is meant by class loader? How many types are there?

0 Answers  


What is java autoboxing?

0 Answers  


List some oops concepts in java?

0 Answers  


Categories