RSS Feed for This PostCurrent Article

Java: Implement Composite Key Lookup for Map

Here is a simple tip to implement composite key lookup for Map, either HashMap or Hashtable, etc

Normally the key to a Map can be a String, Long, Integer, etc. But what if we need to implement our own key, e.g. combination of MSISDN and IMSI.

Here is my SubscriberKey class. It has the composite key which consists of MSISDN and IMSI.

   1:  public class SubscriberKey {
   2:      private String msisdn;
   3:      private String imsi;
   4:   
   5:      public String getMsisdn() {
   6:          return msisdn;
   7:      }
   8:   
   9:      public void setMsisdn(String msisdn) {
  10:          this.msisdn = msisdn;
  11:      }
  12:   
  13:      public String getImsi() {
  14:          return imsi;
  15:      }
  16:   
  17:      public void setImsi(String imsi) {
  18:          this.imsi = imsi;
  19:      }
  20:   
  21:      public boolean equals(Object o) {
  22:          if (this == o) return true;
  23:          if (o == null || getClass()
  24:              != o.getClass()) return false;
  25:   
  26:          SubscriberKey that = (SubscriberKey) o;
  27:   
  28:        if (imsi != null ?

29: !imsi.equals(that.imsi) : that.imsi != null)
  30:                      return false;
  31:          if (msisdn != null ? 
  32:              msisdn.equals(that.msisdn) : 
  33:                  that.msisdn != null) return false;
  34:   
  35:          return true;
  36:      }
  37:   
  38:      public int hashCode() {
  39:          int result;
  40:          result = (msisdn != null ? 
  41:                msisdn.hashCode() : 0);
  42:          result = 31 * result + 
  43:             (imsi != null ? imsi.hashCode() : 0);
  44:          return result;
  45:      }
  46:  }
  47:   
  48:   

I must override the equals and hashCode methods in this class.

Next is my Subscriber class.

   1:   
   2:  public class Subscriber {
   3:      private SubscriberKey key;
   4:      private String name;
   5:      private String category;
   6:      private String email;
   7:   
   8:      public Subscriber() {
   9:          key = new SubscriberKey();
  10:      }
  11:   
  12:      public SubscriberKey getKey() {
  13:          return key;
  14:      }
  15:        
  16:      public void setKey(SubscriberKey key) {
  17:          this.key = key;
  18:      }
  19:   
  20:      public String getName() {
  21:          return name;
  22:      }
  23:   
  24:      public void setName(String name) {
  25:          this.name = name;
  26:      }
  27:   
  28:      public String getCategory() {
  29:          return category;
  30:      }
  31:   
  32:      public void setCategory(String category) {
  33:          this.category = category;
  34:      }
  35:   
  36:      public String getEmail() {
  37:          return email;
  38:      }
  39:   
  40:      public void setEmail(String email) {
  41:          this.email = email;
  42:      }
  43:  }

To test it,

   1:  import java.util.HashMap;
   2:  import java.util.Map;
   3:   
   4:  public class CompositeKeyExample {
   5:   
   6:   
   7:      public static void main(String[] args) {
   8:          Map<SubscriberKey, Subscriber> 
   9:                 subscriberLookup;
  10:          subscriberLookup = 
  11:          new HashMap<SubscriberKey, Subscriber>(2);
  12:   
  13:          SubscriberKey subscriberKey1 = 
  14:                 new SubscriberKey();
  15:          subscriberKey1.setMsisdn("7912333");
  16:          subscriberKey1.setImsi("52445343434343");
  17:          Subscriber subscriber1 = new Subscriber();
  18:          subscriber1.setKey(subscriberKey1);
  19:          subscriber1.setName("peter");
  20:   
  21:          SubscriberKey subscriberKey2 =
  22:               new SubscriberKey();
  23:          subscriberKey2.setMsisdn("1233444");
  24:          subscriberKey2.setImsi("8888888888");
  25:          Subscriber subscriber2 = new Subscriber();
  26:          subscriber2.setKey(subscriberKey2);
  27:          subscriber2.setName("jane");
  28:   
  29:   
  30:          subscriberLookup.put(
  31:               subscriberKey1,subscriber1);
  32:          subscriberLookup.put(
  33:              subscriberKey2,subscriber2);
  34:   
  35:   
  36:          // Do lookup using another composite key
  37:     SubscriberKey subscriberKey3=new SubscriberKey();
  38:          subscriberKey3.setMsisdn("7912333");
  39:          subscriberKey3.setImsi("52445343434343");
  40:   
  41:          Subscriber mySubscriber = 
  42:            subscriberLookup.get(subscriberKey3);
  43:          System.out.println(
  44:                   mySubscriber.getKey().getMsisdn());
  45:          System.out.println(
  46:                  mySubscriber.getKey().getImsi());
  47:          System.out.println(mySubscriber.getName());
  48:   
  49:      }
  50:  }


Trackback URL


RSS Feed for This Post2 Comment(s)

  1. programmer | Apr 2, 2008 | Reply

    Seems good because i had a problem with this requiring a key of timestamp and another id. but i wonder how much impact it got on the performance of the map.get method

  2. ______ | Mar 11, 2009 | Reply

    Hi,
    I think something is missign here:
    31: if (msisdn != null ? 32: msisdn.equals(that.msisdn) : 33: that.msisdn != null) return false;

    I think it would be:
    31: if (msisdn != null ? 32: !msisdn.equal(that.msisdn) :
    33: that.msisdn != null) return false;

Sorry, comments for this entry are closed at this time.