Class Tuple2<A,​B>

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

    public class Tuple2<A,​B>
    extends Object
    implements Map.Entry<A,​B>, UnmodMap.UnEntry<A,​B>, 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​(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
      • getKey

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

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