Uncategorized

Java Maps – Complex Key Example


…so someone asked me today about Java Maps, Oracle Coherence and using complex keys i.e. non-string keys.   The easy part is in declaring your Collection object and storing the value using the complex key object. The tricky part is when you need to get the value back from the map using the complex key.

You do so, i.e. find the stored object, by simply overriding the “equals” method of your complex key class so that the Map interface can compare the object stored and the key you are looking for using the equals method. As mentioned in the documentation for Java Map, care must be taken when using “mutable” objects as keys  – because if you change the value of the fields that make up the key object while it is in the map then you will not be able to find them using the same key values. [See http://download.oracle.com/javase/1.4.2/docs/api/java/util/Map.html]

Here is a quick and dirty example:

 3  import java.util.HashMap;
  4  import java.util.Map;
  5  
  6  public class MapWithComplexKey {
  7  
  8          public static void main(String[] args) {
  9                  ComplexKey one = new MapWithComplexKey.ComplexKey("one", "A");
 10                  ComplexKey two = new MapWithComplexKey.ComplexKey("two", "B");
 11                  ComplexKey three = new MapWithComplexKey.ComplexKey("three", "C");
 12                  ComplexKey weird = new MapWithComplexKey.ComplexKey(null, "thing");
 13  
 14                  Map<ComplexKey, String> testMap = new HashMap<ComplexKey, String>();
 15                  testMap.put(one, one.getCombo());
 16                  testMap.put(two, two.getCombo());
 17                  testMap.put(three, three.getCombo());
 18                  testMap.put(weird, weird.getCombo());
 19  
 20                  // find by key - "one, A"
 21                  ComplexKey finderForOneA = new MapWithComplexKey.ComplexKey("one", "A");
 22                  String valueOneA = testMap.get(finderForOneA);
 23                  System.out.println(" Found " + valueOneA);
 24  
 25                  // find by key - "null, thing"
 26                  ComplexKey finderForNullThing = new MapWithComplexKey.ComplexKey(null,
 27                                  "thing");
 28                  String valueForNullThing = testMap.get(finderForNullThing);
 29                  System.out.println(" Found " + valueForNullThing);
 30  
 31          }
 32  
 33          static class ComplexKey {
 34                  private String first;
 35                  private String last;
 36  
 37                  public boolean equals(Object o) {
 38                          if (o != null && o instanceof ComplexKey) {
 39                                  ComplexKey castedOh = (ComplexKey) o;
 40  
 41                                  if (this.first == null && castedOh.getFirst() == null
 42                                                  && this.last == null && castedOh.getLast() == null) {
 43                                          return true;
 44                                  }
 45  
 46                                  if (this.first != null && castedOh.getFirst() != null
 47                                                  && this.last != null && castedOh.getLast() != null) {
 48                                          if (this.first.equals(castedOh.getFirst())
 49                                                          && this.last.equals(castedOh.getLast())) {
 50                                                  return true;
 51                                          }
 52                                  }
 53  
 54                                  if (this.first == null && castedOh.getFirst() == null) {
 55                                          if (this.last.equals(castedOh.getLast()))
 56                                                  return true;
 57                                  }
 58  
 59                                  if (this.last == null && castedOh.getLast() == null) {
 60                                          if (this.first.equals(castedOh.getFirst()))
 61                                                  return true;
 62                                  }
 63                          }
 64  
 65                          return false;
 66                  }
 67  
 68                  public int hashCode() {
 69                          int hashCode = 7;
 70  
 71                          hashCode = 31 * hashCode + (null == first ? 0 : first.hashCode());
 72                          hashCode = 31 * hashCode + (null == last ? 0 : last.hashCode());
 73  
 74                          return hashCode;
 75                  }
 76  
 77                  public ComplexKey(String first, String last) {
 78                          this.first = first;
 79                          this.last = last;
 80                  }
 81  
 82                  public String getCombo() {
 83                          return (null == first ? "no" : first) + "-"
 84                                          + (null == last ? "no" : last);
 85                  }
 86  
 87                  public String getLast() {
 88                          return last;
 89                  }
 90  
 91                  public void setLast(String last) {
 92                          this.last = last;
 93                  }
 94  
 95                  public String getFirst() {
 96                          return first;
 97                  }
 98  
 99                  public void setFirst(String first) {
100                          this.first = first;
101                  }
102  
103          }
104  }


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s