Skip to content

Instantly share code, notes, and snippets.

@eribeiro
Created October 29, 2012 22:16
Show Gist options
  • Save eribeiro/3976902 to your computer and use it in GitHub Desktop.
Save eribeiro/3976902 to your computer and use it in GitHub Desktop.
Java Pair
public class Pair<U,T> {
public final U left;
public final T right;
public static <U,T> Pair<U,T> of(U u, T t) {
return new Pair<U,T>(u, t);
}
public Pair(U left, T right) {
if (left == null) throw new IllegalArgumentException("left cannot be null");
if (right == null) throw new IllegalArgumentException("right cannot be null");
this.left = left;
this.right = right;
}
@Override public int hashCode() {
return 31 * left.hashCode() + right.hashCode();
}
@Override public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null ) return false;
if (!(obj instanceof Pair)) return false;
Pair p = (Pair) obj;
return left.equals(p.left) && right.equals(p.right);
}
@Override public String toString() {
return "Pair(" + left + "," + right + ")";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment