Created
September 11, 2015 19:19
-
-
Save mh-github/bd4a9f2d9056da327a34 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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