• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with hashes...

 
Rue Fi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to create hashes in Java the same way its done in Perl. I checked out the java API docs but in the java.util I see HashMap, HashSet, and HashTable.
I googled for a quick explanation of how to use these but no help there either.

If anyone can clear up hashes in Java for me or post a helpful link I'd really appreciate it. Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to use HashMap. There are no hash literals as there are in scripting languages like Perl, Python, or Ruby; you have to use explicit method calls. In Java 5:

import java.util.*;
...
Map<String, String> myHash = new HashMap<String, String>();
myHash.put("A", "a");
myHash.put("B", "b");
myHash.put("C", "c");

String myLowerCaseLetter = myHash.get(myUpperCaseLetter);

[ EJFH: Added Garrett's corrections ]
[ July 13, 2006: Message edited by: Ernest Friedman-Hill ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
You want to use HashMap. There are no hash literals as there are in scripting languages like Perl, Python, or Ruby; you have to use explicit method calls. In Java 5:

import java.util.*;
...
Map<String> myHash = new HashMap<String>();
myHash.put("A", "a");
myHash.put("B", "b");
myHash.put("C", "c");

String myLowerCaseLetter = myHash.get(myUpperCaseLetter);


I feel out of place correcting EFH on this one, but it should be:

Map<String, String> myHash = new HashMap<String, String>();
myHash.put("A", "a");
//...
[ July 13, 2006: Message edited by: Garrett Rowe ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Garrett Rowe:


I feel out of place correcting EFH on this one


Never hesitate, especially if my post shows before 7AM Ranch time...
 
Rue Fi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic