• Post Reply Bookmark Topic Watch Topic
  • New Topic

TreeMap get() function not working?  RSS feed

 
Jack Fletcher
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm working on a program which so far reads a text file and counts how many occurences of each character there is in the file.
This is currently achieved using a TreeMap where each character is mapped to a count of how many times it occurs in the text file.

I would like to be able to sum the values mapped to letters of lower case e.g. 'a' to their uppcase counterparts 'A' then display the totals for each.
My understanding is that the .get() function can be used by takin the Character as a key e.g. map.get("A").



I've been toying around with this for a while now and am getting nowhere so any help in the right direction would be greatly appreciated!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I see your mistake:

"A" is a String, 'A' is a char.

You might hope that the compiler would catch this for you, but it turns out the get() method in the Map interface doesn't take the generic type as an argument, it takes a plain Object. That seems a poor design to me, but I've a vague memory of having the reason why it's like that explained to me, and I can't remember what it was (and whether I thought it was a good reason).
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack Fletcher wrote:

I would like to be able to sum the values mapped to letters of lower case e.g. 'a' to their uppcase counterparts 'A' then display the totals for each.
My understanding is that the .get() function can be used by takin the Character as a key e.g. map.get("A").

I've been toying around with this for a while now and am getting nowhere so any help in the right direction would be greatly appreciated!


How about you do this. Write down the steps to accomplish the above in plain English first.
Then write down what each of those steps mean in Java.
Turn it into one/more methods.

Question - What is the current code doing? What does the get method do?

Question and/or a suggestion - Why a TreeMap? Why not a HashMap?

Chan

Edit : Ninja'd :-)
 
Jack Fletcher
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone seems to work now!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!