Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

POJOs as a Map value

 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I recently gave an interview, and the interviewer asked me below question.

If you want to use POJO as a Map values, what things you need to do in the POJO class.

And my answer was like we need to override equals and hashcode method inside the POJO.

Can anybody confirm my answer.

Thanks & Regards,
Jigar Naik.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Map Keys or Map Values? As Map Values, you don't have to do anything special (though equals is usually a good idea anyways). As Map Keys you want to make sure they fit the particular implementation's idea of equality - for HashMap that means hashcode and equals, like you mentioned, but for TreeMap it might mean compartTo instead of hashcode. Also, the Key should be immutable to make sure it can be found in the proper place when it is required.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To use a POJO as a value - nothing - any reference can be a value

To use a POJO as a map key - ensure that equals and hashcode work properly since those methods are used in key lookup.

Bill
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15485
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Map values, you don't have to do anything special, as Steve and William already said.

For Map keys, you have to have properly implemented hashCode() and equals() methods, and there's one more important thing: once you've used an object as a key in a HashMap, you should not change the member variables in the object in a way that would change the result of the hashCode() method. The way hash-based collections store objects is based on the hash code, so if the hash code changes then the collection will not work properly anymore. It's a good idea to make the POJO class immutable to ensure that you can't change the content of the object.
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot steve, bill and jesper. Your responses cleared my doubts as well as fundamental of hashcode and hashmap key values.
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok because the answer has been given multiple times, I will extend the answer because I'm a smartass like that If you intend to serialize your Map, both the key and value have to be serializable.
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:Ok because the answer has been given multiple times, I will extend the answer because I'm a smartass like that If you intend to serialize your Map, both the key and value have to be serializable.



To add to that, the original question also didn't mention that it was a hash map. So...

In the case of a TreeMap, with a comparator, then nothing needs to be done for both the map key or map value.

In the case of a TreeMap, without a comparator, then nothing needs to be done for the map value. For the map key, it needs to support the comparable interface. It also *should* support the equals() method, but interestingly, it is not necessary -- as consistency between equals() and compareTo() methods are not checked.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic