• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HashMaps

 
Ranch Hand
Posts: 91
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just a simple question: Do hashMaps allow duplicate keys? can there be distinct keys but duplicate values? is Hashmap sorted or ordered?
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, that's three questions! ;)
The java.util.HashMap implementation of the java.util.Map interface does not allow duplicate keys (it does allow the null key), distinct keys can map to duplicate values and it is neither ordered nor sorted. If you are looking for a sorted Map implementation you should have a look at java.util.TreeMap.
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.) Existing key-value pair will be replaced by new comer key-value pair, if keys are same... So, You will lose the previous value...

2.) Yes...

3.) Its neither sorted nor ordered... But implicitly it takes the key-value pair based on hashcode in ascending order... So you cant expect them in an order...
 
Komal Arora
Ranch Hand
Posts: 91
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lol yeah Jelle, one question led to another and in no time one became three!

Jelle and Ram, Thanks for the explanation
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic