• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Help with hashes...

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much !
 
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic