The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
September 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.



Posted by Tim W. on September 28, 2001 at 9:48 AM

> Hi all,

Since I don�t have a database, I have created an array using hashmap. I have put some values into each element of the maparray for example: SSN, First Name, Last Name etc. How do I find out the matching map from the maparray using the SSN with each of the array element�s SSN and return the map??

What exactly is it that you're trying to do? If I were going to store a series of Strings that corrosponded with an SSN String, I would create a Map of Maps. The key for the first Map would be the SSN, which would allow you to get the Map containing the info for that SSN. From that Map you can pull out the desired info based on static Strings you're using to identify particular types of data.

Something like this should work for what you want to do, or at least give you an idea. If I've misinterpreted your question, sorry:

public class DataStorage {

public static final String NAME = "name";
public static final String ADDRESS = "address";

HashMap infoBySSN = new HashMap();

public void putData(String SSN, String dataName, String data) {
HashMap personalInfo = (HashMap) infoBySSN.get(SSN);
if (personalInfo == null) {
personalInfo = new HashMap();
personalInfo.put(dataName, data);
infoBySSN.put(SSN, personalInfo);

public String getData(String SSN, String dataName) {
HashMap personalInfo = (HashMap) infoBySSN.get(SSN);
if (personalInfo == null) {
System.out.println("There is no information for this SSN");
return null;
} else {
return (String) personalInfo.get(dataName);


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us