Yes. Create a pair that correctly implements hashCode() and equals() and uses this as the key type. If you use a library like apache commons, you can probably find a pair or tuple there, but otherwise it will work.
Do not overdo it from common pairs. It's good to define a key class for handling the relationship between a pair of elements in a collection, but many people have fundamental objections to the widespread use of paired classes in Java.
public final class PairKey<A, B> { public final A a; public final B b; private PairKey(A a, B b) { this.a = a; this.b = b; } public static <A, B> PairKey<A, B> make(A a, B b) { return new PairKey<A, B>(a, b); } public int hashCode() { return (a != null ? a.hashCode() : 0) + 31 * (b != null ? b.hashCode() : 0); } public boolean equals(Object o) { if (o == null || o.getClass() != this.getClass()) { return false; } PairKey that = (PairKey) o; return (a == null ? that.a == null : a.equals(that.a)) && (b == null ? that.b == null : b.equals(that.b)); } }
and then put the entry for a and b on the card, just do
myMap.put(new PairKey(a, b), value)
source share