aspose file tools*
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 Soft Skills this week in the Jobs Discussion 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: 3611
    
  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: 4490
    
    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: 19062
    
  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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: observations in case of Map