Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

observations in case of Map

 
Ranch Hand
Posts: 203
Eclipse IDE Oracle Java
  • 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..!!
 
Ranch Hand
Posts: 633
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.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • 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 Oracle Java
  • 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..!!
 
Bartender
Posts: 4568
9
  • 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 Oracle Java
  • 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..??
 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
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.
 
Dinner will be steamed monkey heads with a side of tiny ads.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic