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?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are meta-annotations?

733


How to store image in arraylist in java?

712


Why is singleton class used?

773


Explain super keyword in java.

828


How many types of gc are there in java?

751


What is the difference between double and float variables in java?

851


Define an abstract class with reference to java.

776


Why is multithreading important?

695


Is there any way to skip finally block of exception even if some exception occurs in the exception block?

843


What is the purpose of the enableevents() method?

790


Can you make a constructor final?

788


How can you write a loop indefinitely in java programming?

766


What do you mean by static variable?

793


Can a final variable be initialized in constructor?

704


What is logical variable?

723