Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6229146

(coll) Please add java.util.Pair

    Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 6
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
      None

      Description

      Please add a Pair class to java.util. Pairs are often useful
      when storing data in a Map. Getting equals and hashCode right
      is not completely trivial.

      Here is an example of immutable pairs:

      package java.util;

      public class Pair<A,B> {

          private final A first;
          private final B second;

          public Pair(A first, B second) {
      this.first = first;
      this.second = second;
          }

          public getFirst() { return first; }
          public getSecond() { return second; }

          public String toString() {
              return "(" + first + ", " + second + ")";
          }

          private static boolean equals(Object x, Object y) {
      return (x == null && y == null) || (x != null && x.equals(y));
          }

          public boolean equals(Object other) {
          return
      other instanceof Pair &&
      equals(first, ((Pair)other).first) &&
      equals(second, ((Pair)other).second);
          }

          public int hashCode() {
      if (first == null) return (second == null) ? 0 : second.hashCode() + 1;
      else if (second == null) return first.hashCode() + 2;
      else return first.hashCode() * 17 + second.hashCode();
          }
      }

      This example is borrowed from javac and is mostly due to Martin Odersky.

      Alternatively, a Pair interface could be defined with two implementations,
      ImmutablePair and MutablePair.

      ###@###.### 2005-2-15 00:21:11 GMT

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                ahe Peter Ahe
                Reporter:
                ahe Peter Ahe
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: