I have two numbers and I want to use them together as a key in a Map
. Currently, I\'m concatenating their string representations. For example, suppose the key n
Create an object that holds the two numbers and use it as the key. For example:
class Coordinates {
private int x;
private int y;
public Coordinates(int x, int y) {
...
}
// getters
// equals and hashcode using x and y
}
Map locations = new HashMap();
If you prefer a mathematical approach, see this StackOverflow answer.