• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting key/value pairs from HashMaps  RSS feed

 
Jim Venolia
Ranch Hand
Posts: 285
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sigh.  I cruise through the hard stuff only to get caught up in seemingly ez stuff like this.  I'm making making a frequency count of letter, storing them as a hashmap of <char, int>.  Seems when I try to get the key I'm getting the value.  That is, when I call getkey() I get the value of the next key.


The real code doesn't print the key/value pairs, it manipulates the data to decide if a given block of gibberish is text.

help?
 
Ron McLeod
Saloon Keeper
Posts: 1487
210
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some suggestions:

You should be using specific types for Set and Iterator rather than raw (unspecified) types:
    Set<Entry<Character, Integer>> and Iterator<Entry<Character, Integer>>

This line is problematic: Integer key = table.get(me.getKey());
    your key is a Character, not an Integer
    you can get a reference to the key using: Character key = me.getKey(); (don't use table.get() otherwise you will be getting the value)
 
This line is also problematic: Integer value = table.get(me.getValue());
    you can get a reference to the key using: Integer value = me.getValue(); (don't use table.get() otherwise you will be trying to get the value using a value as a key)
 
Also, instead of using an Interator, you could use a for each loop:
Set<Entry<Character, Integer>> set = table.entrySet();
for (Entry<Character, Integer> entry : set) {
   ...
   ...
}

 
Piet Souris
Rancher
Posts: 1885
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jim,

you are mixing generic code ('Map<Character, Integer>') with non-generic code (Set set = ..., Iterator iterator = ..., Map.Entry entry = ...).

Don't do this; stick to generic code all over the place.

Then, if you have a Map.Entry, then this is wasting time:

Why not simply use:


But let's use generics now:

You see, much cleaner code!

A java 8 way could be:
 
Jim Venolia
Ranch Hand
Posts: 285
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Piet, it's not only working but I sort of understand it now.

As for the line: Integer key = table.get(me.getKey());  That was a throwaway debug line to convince myself I was getting the value instead of the key.  You'll notice the commented out line above it is correct, except using char instead of string is better.

As for
you are mixing generic code ('Map<Character, Integer>') with non-generic code (Set set = ..., Iterator iterator = ..., Map.Entry entry = ...).

See, this is the problem with learning by Google.  As a programmer with 30 years experience I know what you mean.  As a newish Java programmer I'm "huh?".  I'd never run across the <> notation before until this morning, and don't know where to go to learn about it.  As to the ..., I don't know if that's you're way of saying "yeah, just like that", or if it's a Java thing I need to learn.  Most of the answers google gives assume more knowledge than I've got, so I have to google to figure out the answer.

As you can probably tell, the code I posted was a mishmash of results from Google searches without really understanding what was going on.  That is the number 1 problem with learning via Google.  With a book or a class you learn topic 1.  Then topic 2, which reinforces topic 1.  Then topic 3, which reinforces topic 2, etc etc etc until you get to the end.

With Google you get topic 3, then topic 12, then topic 23, then topic 1, then topic 18, etc etc etc.  In each topic not only do you not know what you're doing, you don't know that you don't know what you're doing, nor do you know what to google for.
 
Ron McLeod
Saloon Keeper
Posts: 1487
210
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:I'd never run across the <> notation before until this morning, and don't know where to go to learn about it.

There is an Oracle tutorial on Generics here which might help.
 
Jim Venolia
Ranch Hand
Posts: 285
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just noticed Ron also answered and had good advice.  Thanks Ron!

The early bird may get the worm, but the last bird gets credit for emptying the hole

 
Jim Venolia
Ranch Hand
Posts: 285
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just realized I'm still confused.  Do I want to do stuff like "dump(HashMap data") or "dump(HashMap<Character, Integer>, data)?

I'll read that lesson on generics in the morning, when I have time to sit and think.
 
Jim Venolia
Ranch Hand
Posts: 285
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:There is an Oracle tutorial on Generics here which might help.

That was actually quite helpful, Thanks!

/ retired means you read stuff like that on weekends
// and actually enjoy reading it
/// golf?  fishing?  phfft, I'm putting on headphones, staring at the laptop, screw socializing or traveling.
//// sigh.  again, this ain't fark and I gotta quit using slashies
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!