Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

observations in case of Map

 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.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.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you expecting it to throw an exception?
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic