Package javafx.util

Class Pair<L,​R>

  • Type Parameters:
    L - The left type
    R - The right type The identical class (source identical) is vended as two versions:

    Pair and its getLeft() and getRight() methods are fine to use forever. (Although, I'd recommend an immutable type instead if you can get/make it, and to give better context than left/right)

    Pair and its getLeft() and getRight() methods are a band-aid for ClassNotFoundException errors encountered when upgrading to JDK 11 or higher.

    The ClassNotFoundException is tricky. Pair is part of a JavaFX visual effects framework, and that was part of many JDK distributions for a while. The Pair type in particular attracted a bit of usage, but in ways that are unmodeled in package management. (Hey it makes sense, it was just part of the JDK after all.) Now, it's easy enough to upgrade your own code, but if some other dependency was trying to use this tempting little pair class... failure! There's a similar story for library vendors when trying to raise the version required and the effect on your consumers.

    Unless you're trying to get Unless you intend to do visual effects, bringing in the whole JavaFX project will be too much size/hassle/etc for many cases. Therefore, we can use this tiny little dependency as a shim until unneeded.

    A no-dependency alternative is the "Entry" type of Map classes. For example java.util.AbstractMap.SimpleEntry<K,V> or java.util.AbstractMap.SimpleImmutableEntry<K,V>

    All Implemented Interfaces:
    java.io.Serializable

    public class Pair<L,​R>
    extends java.lang.Object
    implements java.io.Serializable
    A pair of two values, a left and a right.
    See Also:
    Serialized Form
    • Constructor Detail

      • Pair

        public Pair​(L left,
                    R right)
        Make a Pair of things.
        Parameters:
        left - The left thing
        right - The right thing
    • Method Detail

      • of

        public static final <L,​R> Pair<L,​R> of​(L left,
                                                           R right)
        Make a Pair of things.
        Type Parameters:
        L - The left type
        R - The right type
        Parameters:
        left - The left thing
        right - The right thing
        Returns:
        a new Pair
      • getLeft

        public L getLeft()
        Get the left value.
        Returns:
        the left value
      • getKey

        @Deprecated
        public L getKey()
        Deprecated.
        Get the left value. An alias for the getLeft() method, the "getKey" convention is to support use cases where Pair was used and may be difficult to untangle.
        Returns:
        the left value
      • getRight

        public R getRight()
        Get the right value.
        Returns:
        the right value
      • getValue

        @Deprecated
        public L getValue()
        Deprecated.
        Get the right value. An alias for the getRight() method, the "getKey" convention is to support use cases where Pair was used and may be difficult to untangle.
        Returns:
        the right value
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object that)
        Overrides:
        equals in class java.lang.Object