• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

ALIASED HASHMAP

For those iof you who know JDK1.3.1 very well:

I am writing a small application where most of the methods involved send/receive lots of parameters. I've decided to encapsulate all of them by sending/receiving HashMap (key-value, dictionary-like structure). So far this has worked for me very well, but now I'm facing a requirement where more than one key must be mapped to the very same value (not copies of this value, nor variables pointing to the same).

Namely, I need a class similar to HashMap that behaves like this:

HashMap hm = new HashMap();
String s1 = hm.get("accountNumber");
String s2 = hm.get("accountNo");
String s3 = hm.get("acctNo");

//Where the three keys are mapped to the very same String value;

Any ideas on how to implement this one?

I hope you guys can help.

Best regards.

Alsvartr
Software Developer.
Legosoft, S.C.

0
alsvartr
Asked:
alsvartr
  • 3
  • 2
  • 2
  • +2
1 Solution
 
CEHJCommented:
Listening..
0
 
objectsCommented:
String value = "xyz";
hm.put("accountNumber", value);
hm.put("accountNo", value);
hm.put("acctNo", value);


0
 
psilli1Commented:
An alternative to above is to have two Maps.

One with the key as above and a value of an identifier for the second Map.

The second Map contains the objects you want to get!!!

i.e.

HashMap a = new HashMap();
HashMap b = new HashMap();

Object o = new SomeObject();

b.put("ACCOUNT", o);

a.put("accountNumber", "ACCOUNT");
a.put("accountNo", "ACCOUNT");
a.put("acctNo", "ACCOUNT");


so now do this:

Object x = (Object)b.get((String)a.get("accountNumber"));

Think this will work, if this is what you want!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
alsvartrAuthor Commented:
Comment to objects:

That would be a rather accurate solution, if my requirement was that all keys should be mapped to the same value (or a copy of it, or a variable containing the same value).

Perhaps I did not explain myself correctly: The behavior I must implement is this:

String ret, value = "foo";
hm.put("key1", value);
hm.putAlias("keyA","key1");
hm.putAlias("keyB","key1");
hm.putAlias("keyC","key1");

// IN THIS BLOCK, ALL get METHODS WOULD RETURN THE VERY SAME STRING VALUE "foo"
ret = hm.get("keyA");
ret = hm.get("keyB");
ret = hm.get("keyC");

I hope I'm being more specific or clear in this question.

Best regards to you all.
0
 
objectsCommented:
Just subclass HashMap:


public class AliasedHashMap extends HashMap
{
   public void putAlias(Object alias, Object kay)
   {
      Object value = get(key);
      put(alias, value);
   }
}
0
 
alsvartrAuthor Commented:
Comment to objects:

That would be a rather accurate solution, if my requirement was that all keys should be mapped to the same value (or a copy of it, or a variable containing the same value).

Perhaps I did not explain myself correctly: The behavior I must implement is this:

String ret, value = "foo";
hm.put("key1", value);
hm.putAlias("keyA","key1");
hm.putAlias("keyB","key1");
hm.putAlias("keyC","key1");

// IN THIS BLOCK, ALL get METHODS WOULD RETURN THE VERY SAME STRING VALUE "foo"
ret = hm.get("keyA");
ret = hm.get("keyB");
ret = hm.get("keyC");

I hope I'm being more specific or clear in this question.

Best regards to you all.
0
 
alsvartrAuthor Commented:
Sorry about that one.

Pressed the wrong key.

I'm coding the solution proposed by objects.

Let's hope it works.

Thanks to you all.
0
 
CEHJCommented:
If in

>>
hm.put("key1", value);
hm.putAlias("keyA","key1");
...etc.
>>

'putAlias' means >>make an alias of key 'key1' called 'keyA'<<

then why is this:

>>if my requirement was that all keys should be mapped to the same value (or a copy of it, or a variable containing the same value).

NOT the case?
0
 
socrateskCommented:
Try this one.


//Subclass the HashMap class first. Modify the casting stuff if you want an object.
import java.util.*;
public class SubHashMap extends HashMap{

   public Object get(Object key) {

      String strValue  = (String)super.get(key);
      String actualVal =  (String)super.get(strValue);

      return (Object)actualVal;
   }
}


//Actual stuff starts here.
class TestHash{

   public static void main (String srgs []) {

      HashMap hm = new SubHashMap();
      hm.put("acNo", "12345");

      hm.put("accountNumber", "acNo");
      hm.put("accountNo", "acNo");
      hm.put("acctNo", "acNo");

      System.out.println(hm.get("accountNumber"));
      System.out.println(hm.get("accountNo"));
      System.out.println(hm.get("acctNo"));
   }
}


Hope the above code(s) will solve your problem.

Socrates
Chennai-India
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now