Skip to content

Instantly share code, notes, and snippets.

@mh-github
Created September 11, 2015 19:19
Show Gist options
  • Select an option

  • Save mh-github/bd4a9f2d9056da327a34 to your computer and use it in GitHub Desktop.

Select an option

Save mh-github/bd4a9f2d9056da327a34 to your computer and use it in GitHub Desktop.
public class Pair implements Comparable<Pair> {
private final int key;
private final float value;
public Pair(int key, float value) {
this.key = key;
this.value = value;
}
public int getKey()
{
return key;
}
public float getValue()
{
return value;
}
@Override
public int compareTo(Pair that) {
int cmp = Float.compare(this.value, that.value);
if (cmp < 0)
return -1;
else if (cmp > 0)
return 1;
else
return 0;
}
// is this Pair equal to x?
public boolean equals(Object x) {
if (x == this) return true;
if (x == null) return false;
if (x.getClass() != this.getClass()) return false;
Pair that = (Pair) x;
return (this.value == that.value);
}
public int hashCode() {
int hash = 17;
hash = 31*hash + ((Integer) key).hashCode();
hash = 31*hash + ((Float) value).hashCode();
return hash;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment