File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help with hashes... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with hashes..." Watch "Help with hashes..." New topic
Author

Help with hashes...

Rue Fi
Greenhorn

Joined: Jul 08, 2006
Posts: 7
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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 ]

[Jess in Action][AskingGoodQuestions]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Jul 08, 2006
Posts: 7
Thank you very much !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with hashes...