Class Tuple2<A,​B>

  • All Implemented Interfaces:
    java.io.Serializable, java.util.Map.Entry<A,​B>, UnmodMap.UnEntry<A,​B>

    public class Tuple2<A,​B>
    extends java.lang.Object
    implements java.util.Map.Entry<A,​B>, UnmodMap.UnEntry<A,​B>, java.io.Serializable
    Holds 2 items of potentially different types, and implements Map.Entry (and UnmodMap.UnEntry (there is no ImMap.ImEntry)). Designed to let you easily create immutable subclasses (to give your data structures meaningful names) with correct equals(), hashCode(), and toString() methods.
    See Also:
    Serialized Form
    • Field Detail

      • _1

        protected final A _1
      • _2

        protected final B _2
    • Constructor Detail

      • Tuple2

        protected Tuple2​(A a,
                         B b)
        Constructor is protected (not public) for easy inheritance. Josh Bloch's "Item 1" says public static factory methods are better than constructors because they have names, they can return an existing object instead of a new one, and they can return a sub-type. Therefore, you have more flexibility with a static factory as part of your public API then with a public constructor.
    • Method Detail

      • of

        public static <A,​B> Tuple2<A,​B> of​(A a,
                                                       B b)
        Public static factory method
      • of

        public static <K,​V> Tuple2<K,​V> of​(java.util.Map.Entry<K,​V> entry)
        Map.Entry factory method
      • _1

        public A _1()
        Returns the 1st field
      • _2

        public B _2()
        Returns the 2nd field
      • toString

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

        public boolean equals​(java.lang.Object other)
        Specified by:
        equals in interface java.util.Map.Entry<A,​B>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Map.Entry<A,​B>
        Overrides:
        hashCode in class java.lang.Object
      • getKey

        public A getKey()
        Returns the first field of the tuple. To implement Map.Entry.
        Specified by:
        getKey in interface java.util.Map.Entry<A,​B>
      • getValue

        public B getValue()
        Returns the second field of the tuple. To implement Map.Entry.
        Specified by:
        getValue in interface java.util.Map.Entry<A,​B>
      • setValue

        @Deprecated
        public B setValue​(B value)
        Deprecated.
        This method is required to implement Map.Entry, but calling it only issues an exception
        Specified by:
        setValue in interface java.util.Map.Entry<A,​B>
        Specified by:
        setValue in interface UnmodMap.UnEntry<A,​B>