Can we place the any object to key attribute of
HashMap<key,value>?

Answers were Sorted based on User's Feedback



Can we place the any object to key attribute of HashMap<key,value>?..

Answer / abhijeet

Hi,
I am giving you a collective answer. Yes you can use any
object as key in hashmap. But if you r sure that u will be
using diferent objects as key then u dont ve to overide
anything. But if you are going to use same object as key
then you need to override hashcode and equals method. Be
sure that your equals method returns false if you need to
use the object as key.

But this is tricky 1 and in live enviroment its not needed :-)
only for interview to impress interviewwer :-)

Is This Answer Correct ?    8 Yes 1 No

Can we place the any object to key attribute of HashMap<key,value>?..

Answer / ramarao katta

yes, we can place any object as key value but that object
must and should implement the hashcode method.

Is This Answer Correct ?    5 Yes 2 No

Can we place the any object to key attribute of HashMap<key,value>?..

Answer / patil abhijeet

Hi Ramarao,
Its not necessary to override hashCode() since hashcode is
default generated for every object. Please try my example n
check it by urself

Is This Answer Correct ?    4 Yes 2 No

Can we place the any object to key attribute of HashMap<key,value>?..

Answer / patil abhijeet

Yes we can use object as the key

person p = new person();

HashMap m = new HashMap()
m.put(p,"mysqlf");
above works perfectly fy9 ..... but dont try to use the same
object as key twice :-)

Is This Answer Correct ?    5 Yes 6 No

Post New Answer

More Core Java Interview Questions

How is it possible in java programming for two string objects with identical values not to be equal under the == operator?

0 Answers  


What is the use of predicate in java 8?

0 Answers  


What is internal variable?

0 Answers  


What do you understand by copy constructor in java?

0 Answers  


What is meant by final class, methods and variables?

3 Answers  






what is the output??????? public class multireturn { public(int assign x ( int x) { if(4==x) { return 7; } else if (7=x+3) { return 6; } return 5; } }

3 Answers   TCS,


Can we declare register variable as global?

0 Answers  


If there are two notepad opened and working on it . is this a process or thread? computer based Live Example to differentiate between Thread , Process , and Multi tasking ?

3 Answers   TCS, Wipro,


what invokes a threads run() method? : Java thread

0 Answers  


What is a java developer salary?

0 Answers  


What is the default value of the local variables?

0 Answers  


What is a nested class?

0 Answers  


Categories