fork download
  1. import java.util.Comparator;
  2. import java.util.TreeSet;
  3.  
  4. class Ideone {
  5. public static void main(String[] args) {
  6. final TreeSet<Pair<Integer, Integer>> sortedSet = new TreeSet<>(Comparator
  7. .comparingInt(Pair<Integer, Integer>::getValue).reversed()
  8. .thenComparing(Pair::getKey));
  9. sortedSet.add(new Pair<>(4, 51));
  10. sortedSet.add(new Pair<>(8, 85));
  11. sortedSet.add(new Pair<>(1, 16));
  12. sortedSet.add(new Pair<>(2, 51));
  13. System.out.println(sortedSet);
  14. }
  15. }
  16.  
  17. class Pair<K, V> {
  18.  
  19. /**
  20.   * Key of this <code>Pair</code>.
  21.   */
  22. private K key;
  23.  
  24. /**
  25.   * Gets the key for this pair.
  26.   *
  27.   * @return key for this pair
  28.   */
  29. public K getKey() {
  30. return key;
  31. }
  32.  
  33. /**
  34.   * Value of this this <code>Pair</code>.
  35.   */
  36. private V value;
  37.  
  38. /**
  39.   * Gets the value for this pair.
  40.   *
  41.   * @return value for this pair
  42.   */
  43. public V getValue() {
  44. return value;
  45. }
  46.  
  47. /**
  48.   * Creates a new pair
  49.   *
  50.   * @param key The key for this pair
  51.   * @param value The value to use for this pair
  52.   */
  53. public Pair(K key, V value) {
  54. this.key = key;
  55. this.value = value;
  56. }
  57.  
  58. /**
  59.   * <p><code>String</code> representation of this
  60.   * <code>Pair</code>.</p>
  61.   *
  62.   * <p>The default name/value delimiter '=' is always used.</p>
  63.   *
  64.   * @return <code>String</code> representation of this <code>Pair</code>
  65.   */
  66. @Override
  67. public String toString() {
  68. return key + "=" + value;
  69. }
  70.  
  71. /**
  72.   * <p>Generate a hash code for this <code>Pair</code>.</p>
  73.   *
  74.   * <p>The hash code is calculated using both the name and
  75.   * the value of the <code>Pair</code>.</p>
  76.   *
  77.   * @return hash code for this <code>Pair</code>
  78.   */
  79. @Override
  80. public int hashCode() {
  81. int hash = 7;
  82. hash = 31 * hash + (key != null ? key.hashCode() : 0);
  83. hash = 31 * hash + (value != null ? value.hashCode() : 0);
  84. return hash;
  85. }
  86.  
  87. /**
  88.   * <p>Test this <code>Pair</code> for equality with another
  89.   * <code>Object</code>.</p>
  90.   *
  91.   * <p>If the <code>Object</code> to be tested is not a
  92.   * <code>Pair</code> or is <code>null</code>, then this method
  93.   * returns <code>false</code>.</p>
  94.   *
  95.   * <p>Two <code>Pair</code>s are considered equal if and only if
  96.   * both the names and values are equal.</p>
  97.   *
  98.   * @param o the <code>Object</code> to test for
  99.   * equality with this <code>Pair</code>
  100.   * @return <code>true</code> if the given <code>Object</code> is
  101.   * equal to this <code>Pair</code> else <code>false</code>
  102.   */
  103. @Override
  104. public boolean equals(Object o) {
  105. if (this == o) {
  106. return true;
  107. }
  108. if (o instanceof Pair) {
  109. Pair pair = (Pair) o;
  110. if (key != null ? !key.equals(pair.key) : pair.key != null) {
  111. return false;
  112. }
  113. if (value != null ? !value.equals(pair.value) : pair.value != null) {
  114. return false;
  115. }
  116. return true;
  117. }
  118. return false;
  119. }
  120. }
Success #stdin #stdout 0.21s 50736KB
stdin
Standard input is empty
stdout
[8=85, 2=51, 4=51, 1=16]