Guest User

Untitled

a guest
May 22nd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. public class MyKey implements Comparable<MyKey>
  2. {
  3. private Integer i;
  4. private String s;
  5. public MyKey(Integer i,String s)
  6. {
  7. this.i=i;
  8. this.s=s;
  9. }
  10.  
  11. public Integer getI() { return i;}
  12. public String getS() { return s;}
  13.  
  14. @Override
  15. public int hashcode()
  16. {
  17. return i.hashcode()+31*s.hashcode();
  18. }
  19.  
  20. @Override
  21. public boolean equals(Object o)
  22. {
  23. if(o==this) return true;
  24. if(o==null || !(o instanceof MyKey)) return false;
  25. MyKey cp= MyKey.class.cast(o);
  26. return i.equals(cp.i) && s.equals(cp.s);
  27. }
  28.  
  29. public int compareTo(MyKey cp)
  30. {
  31. if(cp==this) return 0;
  32. int i= i.compareTo(cp.i);
  33. if(i!=0) return i;
  34. return s.compareTo(cp.s);
  35. }
  36.  
  37.  
  38. @Override
  39. public String toString()
  40. {
  41. return "("+i+";"+s+")";
  42. }
  43.  
  44. }
  45.  
  46. public Map<MyKey,String> map= new HashMap<MyKey,String>();
  47. map.put(new MyKey(1,"Hello"),"world");
  48.  
  49. map.put(Arrays.asList(keyClass, keyString), value)
  50.  
  51. public class Key {
  52.  
  53. public MyClass key_class;
  54. public String key_string;
  55.  
  56. public Key(){
  57. key_class = new MyClass();
  58. key_string = "";
  59. }
  60.  
  61. }
  62.  
  63. public class KeyClass {
  64.  
  65. private String element1;
  66. private String element2;
  67.  
  68. //boilerplate code here
  69.  
  70. public boolean equals(Object obj) {
  71. if (obj instanceof KeyClass) {
  72. return element1.equals(((KeyClass)obj).element1) &&
  73. element2.equals(((KeyClass)obj).element2);
  74. }
  75. return false;
  76. }
  77.  
  78. public int hashcode() {
  79. return (element1 + element2).hashcode();
  80. }
  81. }
  82.  
  83. Map<Key1, value>
  84.  
  85. Map<Key2, value>
  86.  
  87. Map<Key1, Map<Key2, value>>
  88.  
  89. class KeyHolder {
  90. public final String key;
  91. public final Object storeMe;
  92.  
  93. public KeyHolder(String key, Object storeMe) {
  94. this.key=key;
  95. this.storeMe=storeMe;
  96. }
  97.  
  98. public equals(Object o) {
  99. return (o instanceof KeyHolder && ((KeyHolder)o).key.equals(key));
  100. }
  101.  
  102. public hashcode() {
  103. return key.hashCode();
  104. }
  105. }
Add Comment
Please, Sign In to add comment