Class Pair<T>

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<T>, java.util.Collection<T>

    public final class Pair<T>
    extends java.lang.Object
    implements java.util.Collection<T>, java.io.Serializable
    An implementation of Collection that stores exactly 2 non-null objects and is not mutable. They respect equals and may be used as indices or map keys.

    Note that they do not protect from malevolent behavior: if one or another object in the tuple is mutable, then it can be changed with the usual bad effects.

    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      Pair​(java.util.Collection<? extends T> values)
      Creates a Pair from the passed Collection.
      Pair​(T[] values)
      Creates a Pair from the passed array.
      Pair​(T value1, T value2)
      Creates a Pair from the specified elements.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(T o)  
      boolean addAll​(java.util.Collection<? extends T> c)  
      void clear()  
      boolean contains​(java.lang.Object o)  
      boolean containsAll​(java.util.Collection<?> c)  
      boolean equals​(java.lang.Object o)  
      T getFirst()  
      T getSecond()  
      int hashCode()  
      boolean isEmpty()  
      java.util.Iterator<T> iterator()  
      boolean remove​(java.lang.Object o)  
      boolean removeAll​(java.util.Collection<?> c)  
      boolean retainAll​(java.util.Collection<?> c)  
      int size()  
      java.lang.Object[] toArray()  
      <S> S[] toArray​(S[] a)  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, spliterator, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
    • Constructor Detail

      • Pair

        public Pair​(T value1,
                    T value2)
        Creates a Pair from the specified elements.
        Parameters:
        value1 - the first value in the new Pair
        value2 - the second value in the new Pair
        Throws:
        java.lang.IllegalArgumentException - if either argument is null
      • Pair

        public Pair​(java.util.Collection<? extends T> values)
        Creates a Pair from the passed Collection. The size of the Collection must be 2.
        Parameters:
        values - the elements of the new Pair
        Throws:
        java.lang.IllegalArgumentException - if the input collection is null, contains null values, or has != 2 elements.
      • Pair

        public Pair​(T[] values)
        Creates a Pair from the passed array. The size of the array must be 2.
        Parameters:
        values - the values to be used to construct this Pair
        Throws:
        java.lang.IllegalArgumentException - if the input array is null, contains null values, or has != 2 elements.
    • Method Detail

      • getFirst

        public T getFirst()
        Returns:
        the first element.
      • getSecond

        public T getSecond()
        Returns:
        the second element.
      • equals

        public boolean equals​(java.lang.Object o)
        Specified by:
        equals in interface java.util.Collection<T>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Collection<T>
        Overrides:
        hashCode in class java.lang.Object
      • toString

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

        public boolean add​(T o)
        Specified by:
        add in interface java.util.Collection<T>
      • addAll

        public boolean addAll​(java.util.Collection<? extends T> c)
        Specified by:
        addAll in interface java.util.Collection<T>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<T>
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<T>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> c)
        Specified by:
        containsAll in interface java.util.Collection<T>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<T>
      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.util.Collection<T>
        Specified by:
        iterator in interface java.lang.Iterable<T>
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<T>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> c)
        Specified by:
        removeAll in interface java.util.Collection<T>
      • retainAll

        public boolean retainAll​(java.util.Collection<?> c)
        Specified by:
        retainAll in interface java.util.Collection<T>
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<T>
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<T>
      • toArray

        public <S> S[] toArray​(S[] a)
        Specified by:
        toArray in interface java.util.Collection<T>