Skip to content

Instantly share code, notes, and snippets.

@jerolba
Created February 7, 2019 20:07
Show Gist options
  • Save jerolba/2eefdc50b64247a3ab165baa5af3d4de to your computer and use it in GitHub Desktop.
Save jerolba/2eefdc50b64247a3ab165baa5af3d4de to your computer and use it in GitHub Desktop.
Object with composed key
public class MyObject {
private MyObjectKey key;
private String someAttribute;
public MyObject(int firstKey, int secondKey, String someAttribute) {
this.key = new MyObjectKey(firstKey, secondKey);
this.someAttribute = someAttribute;
}
public MyObjectKey getKey() { return key; }
public String getSomeAttribute() { return someAttribute; }
public int hashCode() { return key.hashCode(); }
public boolean equals(Object obj) { ... }
}
public class MyObjectKey {
private int firstKey;
private int secondKey;
public MyObjectKey(int firstKey, int secondKey) {
this.firstKey = firstKey;
this.secondKey = secondKey;
}
public int getFirstKey() { return firstKey; }
public int getSecondKey() { return secondKey; }
public int hashCode() {
int result = 31 + firstKey;
return 31 * result + secondKey;
}
public boolean equals(Object obj) { ... }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment