Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Hash Mapping

 
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


 
Marshal
Posts: 69854
278
  • 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: 69854
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
… and you can map Strings to Integers, too.
 
Bartender
Posts: 7202
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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: 69854
278
  • 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.
 
Marshal
Posts: 25682
69
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: 69854
278
  • 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.
 
The knights of nee want a shrubbery. And a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic