wood burning stoves 2.0*
The moose likes Java in General and the fly likes observations in case of Map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "observations in case of Map" Watch "observations in case of Map" New topic
Author

observations in case of Map

Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Hi Folks,,
I was going through Map ...I have observed in my below piece of code that if we try to have same key then the latest key overrides the value of previous key while iterating..Now my query is that can Map accept duplicate keys that is ok ...but does it accept duplicate keys with duplicate values..can you advise me in which case the map throws the error if we play with Map...!!



The result that I am getting ..


There are 6 elements in the map.
Content of Map are...
Nitin PHP
Spring1
Anupam Hibernate
Ravi .Net
Amit Java
Saral Andriod


Please advise..!!
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

.Now my query is that can Map accept duplicate keys that is ok ...but does it accept duplicate keys with duplicate values..can you advise me in which case the map throws the error if we play with Map...!!


Yes it accept duplicate keys with duplicate values.By definition, the put command replaces the previous value associated with the given key in the map.


Life is easy because we write the source code.....
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

Please see documentation of the java.util.Map interface. I'd also strongly suggest reading the Collections Framework tutorial, specifically this page.
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Pramod P Deore wrote:
.Now my query is that can Map accept duplicate keys that is ok ...but does it accept duplicate keys with duplicate values..can you advise me in which case the map throws the error if we play with Map...!!


Yes it accept duplicate keys with duplicate values.By definition, the put command replaces the previous value associated with the given key in the map.


Yeah but please guide me in which cases Map can throw exceptions..!! thanks in advance..!!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Why are you expecting it to throw an exception?
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Matthew Brown wrote:Why are you expecting it to throw an exception?


Just to enhance my knowledge ...is there any case in which Map throws exception..??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

Saral Saxena wrote:
Matthew Brown wrote:Why are you expecting it to throw an exception?


Just to enhance my knowledge ...is there any case in which Map throws exception..??



For questions like these, the javadoc is the best place to look -- java.util.Map.

Henry
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Saral Saxena wrote:
Matthew Brown wrote:Why are you expecting it to throw an exception?


Just to enhance my knowledge ...is there any case in which Map throws exception..??


In addition to reading the javadocs as already suggested, keep in mind that any method in class can throw an unchecked exception any time you call it.
 
Consider Paul's rocket mass heater.
 
subject: observations in case of Map
 
Similar Threads
Get keys on the basis of values in any Collection
Need logic: Workaround to have duplicate keys in Hashmap
Example on Map.Entry methods
re: Iteration in a Map
query regarding null key in hashmap