• 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

duplicate keys in map and the keys must be sorted

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to make a program: read a file from the hard disk and count the number of words in the file. The programmer must then print the words along with the number of times the word has occured in the file in order of their occurence.
I have counted the number of times the word has occured in the file but I am not able to print them in ascending order of their occurence. For e.g. if words are:
'student' occured 2 times
'child' occured 1 time
'parent' occured 1 time, then the output should be

child: 1
parent :1
student :2

I think map can be the best way to store this. I tried using TreeMap so that it can store the key ( number of occurence) in ascending order. But it does not store duplicate keys!!!


I mean my output is coming as:

child: 1
student :2

the entry :
parent :1
is missing as the key "1" is duplicate.
How can I maintain duplicate keys in map so that I can use them in ascending order?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't have duplicate keys in a Map, that is part of the definition of Map.

If it were me, I would make a Map<Word, Count>, rather than Map<Count, Word>. Then, when I wanted to show a Sorted View, I would take the Entry Set, and add it to a TreeSet<Map.Entry<Word,Count>>. I would make the TreeSet with a Comparator<Map.Entry<Word, Count>> which took the Map.Entry's Value (count) and ordered them according to that.

Pertinent Javadocs:
TreeSet
Map#entrySet()
Comparator
Map.Entry
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carox:

By definition, map keys have to be unique.

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

If it were me, I would make a Map<Word, Count>, rather than Map<Count, Word>. Then, when I wanted to show a Sorted View, I would take the Entry Set, and add it to a TreeSet<Map.Entry<Word,Count>>. I would make the TreeSet with a Comparator<Map.Entry<Word, Count>> which took the Map.Entry's Value (count) and ordered them according to that.


Thanks for the logic. So you mean you would have done:

But then how the comparator can be speciified? In the construtor the set is passed.

 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would reverse it so the key is the word, and the counter is value.

For the TreeSet, because of there is no constructor that allows me to provide both the comparator AND the initial values, I would call the TreeSet constructor that takes a comparator, and then use addAll to put in the entrySet. Something like this:

 
reply
    Bookmark Topic Watch Topic
  • New Topic