• 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

TreeMap get() function not working?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone seems to work now!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic