Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyKey implements Comparable<MyKey>
- {
- private Integer i;
- private String s;
- public MyKey(Integer i,String s)
- {
- this.i=i;
- this.s=s;
- }
- public Integer getI() { return i;}
- public String getS() { return s;}
- @Override
- public int hashcode()
- {
- return i.hashcode()+31*s.hashcode();
- }
- @Override
- public boolean equals(Object o)
- {
- if(o==this) return true;
- if(o==null || !(o instanceof MyKey)) return false;
- MyKey cp= MyKey.class.cast(o);
- return i.equals(cp.i) && s.equals(cp.s);
- }
- public int compareTo(MyKey cp)
- {
- if(cp==this) return 0;
- int i= i.compareTo(cp.i);
- if(i!=0) return i;
- return s.compareTo(cp.s);
- }
- @Override
- public String toString()
- {
- return "("+i+";"+s+")";
- }
- }
- public Map<MyKey,String> map= new HashMap<MyKey,String>();
- map.put(new MyKey(1,"Hello"),"world");
- map.put(Arrays.asList(keyClass, keyString), value)
- public class Key {
- public MyClass key_class;
- public String key_string;
- public Key(){
- key_class = new MyClass();
- key_string = "";
- }
- }
- public class KeyClass {
- private String element1;
- private String element2;
- //boilerplate code here
- public boolean equals(Object obj) {
- if (obj instanceof KeyClass) {
- return element1.equals(((KeyClass)obj).element1) &&
- element2.equals(((KeyClass)obj).element2);
- }
- return false;
- }
- public int hashcode() {
- return (element1 + element2).hashcode();
- }
- }
- Map<Key1, value>
- Map<Key2, value>
- Map<Key1, Map<Key2, value>>
- class KeyHolder {
- public final String key;
- public final Object storeMe;
- public KeyHolder(String key, Object storeMe) {
- this.key=key;
- this.storeMe=storeMe;
- }
- public equals(Object o) {
- return (o instanceof KeyHolder && ((KeyHolder)o).key.equals(key));
- }
- public hashcode() {
- return key.hashCode();
- }
- }
Add Comment
Please, Sign In to add comment