Created
February 7, 2019 20:07
-
-
Save jerolba/2eefdc50b64247a3ab165baa5af3d4de to your computer and use it in GitHub Desktop.
Object with composed key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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