?
Solved

ALIASED HASHMAP

Posted on 2003-02-25
9
Medium Priority
?
261 Views
Last Modified: 2010-03-31
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
Comment
Question by:alsvartr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8020244
Listening..
0
 
LVL 92

Expert Comment

by:objects
ID: 8020894
String value = "xyz";
hm.put("accountNumber", value);
hm.put("accountNo", value);
hm.put("acctNo", value);


0
 
LVL 1

Expert Comment

by:psilli1
ID: 8021071
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:alsvartr
ID: 8021189
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
 
LVL 92

Expert Comment

by:objects
ID: 8021262
Just subclass HashMap:


public class AliasedHashMap extends HashMap
{
   public void putAlias(Object alias, Object kay)
   {
      Object value = get(key);
      put(alias, value);
   }
}
0
 

Author Comment

by:alsvartr
ID: 8021321
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
 

Author Comment

by:alsvartr
ID: 8021345
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8021466
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
 
LVL 1

Accepted Solution

by:
socratesk earned 400 total points
ID: 8023286
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question