wood burning stoves 2.0*
The moose likes Java in General and the fly likes duplicate keys in map and the keys must be sorted Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "duplicate keys in map and the keys must be sorted" Watch "duplicate keys in map and the keys must be sorted" New topic
Author

duplicate keys in map and the keys must be sorted

carox kaur
Ranch Hand

Joined: Mar 19, 2009
Posts: 52
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?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

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


Steve
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Carox:

By definition, map keys have to be unique.

John.
carox kaur
Ranch Hand

Joined: Mar 19, 2009
Posts: 52
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

Joined: Jan 28, 2003
Posts: 4167
    
  21

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:

 
 
subject: duplicate keys in map and the keys must be sorted
 
Similar Threads
gettin error with TextReader
Find the maximum occurence of words in a file
Finding Most Common Phrase Occurance In String?
Ideal Collection implementation to be used for counting the occurance of a word in a file.
TreeMap question#1