• 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

Repeated Key algorithm obtained from net can't detect user class keys?

 
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

All occurrencies happen to be one only no matter they have repeated several times.
Any ideas why?
Thanks
Jack

 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hashmap stores the key by calculating hashes. For same key hash always same so it always getting replaces your old entry with the new entry.
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there,
I have changed to String as keys, and it works well.
But I've got a new question.
How do I design the algorithm such as the following happens

I can use a SortedSet for that, but now I want to count how many occurrencies happen
for 1:1, 1:2 etc after the Set has been filtered. (The second and the third keys)
How can I do that?
Thanks
Jack
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot expect a hash map to work unless the “K”s are immutable, with correctly‑overridden equals() and hashCode() methods. Does your path point class fulfil those requirements?
 
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should use a strong type for your key and not a String. You should implement the equals() and hashCode() methods on your key type properly. Please make your key type immutable. If you're using Java 8, you can use Map.merge() to add entries to a map, and handle the situation where the entry already exists graciously: occurences.merge(word, 1, (a,b) -> a+b);
 
reply
    Bookmark Topic Watch Topic
  • New Topic