• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hash Mapping  RSS feed

 
Callum Hughes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a Example of a Hash Mapping Algorithm in Java.

This Mapping Needs to do the following:
Map an Array of Strings, to integer values which correspond to each fruit



private String[] Fruit = {"Apple","Orange","Pear","Grapes"};

Apple: 1 + 1
---------------
Orange: 1+ 2
---------------
Pear: 1 + 3
---------------
Grapes: 1 + 4
---------------


I not sure where to start at the Moment any Help much be Appreciated Thanks :P


 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

How much do you know about Maps? There is an introduction in the Java® Tutorials. If you want to map an Integer to a String that is quite possible. Just remember that if you map two different things with the same “K”, you will end up overwriting the original “V”.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
… and you can map Strings to Integers, too.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure from your description whether your trying to understand the algorithm behind hash maps or trying to understand how to make use of HashMaps in your code. Either way you are not giving us much code as an example. Have you read the java.util.HashMap javadoc? Have you googled "java hashmap tutorial" ?
 
Callum Hughes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

How much do you know about Maps? There is an introduction in the Java® Tutorials. If you want to map an Integer to a String that is quite possible. Just remember that if you map two different things with the same “K”, you will end up overwriting the original “V”.


Hi, yes the link was sort of useful, is there any other resources with simpler breakdowns and / or any no pre-defined library java examples that I can look at to give me a better understanding. Thanks pal
 
Callum Hughes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:I'm not sure from your description whether your trying to understand the algorithm behind hash maps or trying to understand how to make use of HashMaps in your code. Either way you are not giving us much code as an example. Have you read the java.util.HashMap javadoc? Have you googled "java hashmap tutorial" ?


Sorry about that, I dont want to use any predefined java libraries.
The lack of code is due to myself not knowing how to correctly format it

A Code Example solving the problem or a similar problem would be very helpful.

Thanks
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not exactly clear to me what you want to do with the map.

A Map is like a phone book; it contains entries that each have a key and a value, and you can lookup the value if you know the key. In case of a phone book, the key is the name of a person, and the value is the phone number. You can quickly lookup the phone number if you know the person's name.

Do you want a Map where the keys are names of types of fruit, and the value is an integer? That would look something like this:

See The Map Interface in Oracle's Java Tutorials.
 
Callum Hughes
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:It's not exactly clear to me what you want to do with the map.

A Map is like a phone book; it contains entries that each have a key and a value, and you can lookup the value if you know the key. In case of a phone book, the key is the name of a person, and the value is the phone number. You can quickly lookup the phone number if you know the person's name.

Do you want a Map where the keys are names of types of fruit, and the value is an integer? That would look something like this:

See The Map Interface in Oracle's Java Tutorials.


Thanks for your Help Jesper!
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want to write your own Map implementation? I would advise against that even for an exercise, unless you have a week to spare.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Callum Hughes wrote:This Mapping Needs to do the following:
Map an Array of Strings, to integer values which correspond to each fruit

private String[] Fruit = {"Apple","Orange","Pear","Grapes"};



Well, normally a hash function would map each of the Strings to an integer value. It wouldn't map the array to anything. If you want a simple example of mapping a String to an integer value, here's one:



This maps each of the Strings to the integer value 0.

You may be thinking, that isn't very good because it doesn't map different Strings to different integer values. But that isn't a requirement for a hash function, and those documents you've been pointed to should have mentioned that. Do your requirements require different hash values for each input? If so, that's a very specialized form of hash function and you can really only achieve it if you know in advance what keys are going to be used.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote: . . . those documents you've been pointed to should have mentioned that. . . .
I linked Effective Java™ (old edition) in his other thread, and that definitely mentions that problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!