• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with HashMaps

 
Michael Duff
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there, I am looking for help with HashMaps.

I got the following code for initializing a HashMap (which is for a language recognizing program). The first HashMap contains the language as key (String), and another hashmap as value. The second hashmap has the letters (in ASCII) as key, and a Vector as value. The Vector will hold the amount of occurences of each letter. I am asking if this is the correct way to initialize it:



And is this the correct way of adding new languages and accessing the components in that kind of HashMap structure?

Also I'd like to ask if there are any alternative ways of achieving the same kind of linked structure?

Cheers,

Michael
 
Janeice DelVecchio
Saloon Keeper
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a similar problem.... I can't find the thread now, but I think the way I fixed the compiler issue I was getting is to get rid of the internal generic declaration on the right side of the expression.

The compiler just cares what the list or map is populated with. If it's populated with lists or maps, great... but it doesn't need to know what the internal list or map has in it.

so this won't work:

But this should:


I think the reasoning for this is the way the declarations of generics work and where they're needed...

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Welcome to JavaRanch
 
Michael Duff
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, and thanks Janeice, think that makes the structure a bit clearer.

But now I face another problem that is Vector, is there any way to make the indexing of the Vector start from something else than 0? I am looking for something like this:



Where count is the amount of a specific character in a text, and percent is the percentage a specific character represents in a text. All I'm getting is IndexOutOfBoundsExceptions... and I seem to be very poor at finding information from internet because I couldn't find anything for this exact problem.

Cheers,

Michael
 
Janeice DelVecchio
Saloon Keeper
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... I don't know much about vectors.... why don't you start a new thread? You'll probably get more responses that way.

And you're welcome!
 
Bert Wilkinson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not a great use of a Vector. A Vector is basically just a resizeable array. It sounds like you just want another map, using character as a key and a statistical object as the value. Consider making a simple class to hold the data you want and putting that into your map. Like:


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic