File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes POJOs as a Map value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "POJOs as a Map value" Watch "POJOs as a Map value" New topic
Author

POJOs as a Map value

Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 759
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.

Jigar Naik


Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12821
    
    5
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

Joined: Aug 16, 2005
Posts: 14338
    
  22

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 759
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
Bartender

Joined: Jan 17, 2008
Posts: 2431
    
  28

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
Sheriff

Joined: Sep 28, 2004
Posts: 18968
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: POJOs as a Map value