Pair

public abstract class org.apache.commons.lang3.tuple.Pair<L, R> implements Map.Entry<L, R>, Comparable<Pair<L, R>>, Serializable[source]

A pair consisting of two elements.

This class is an abstract implementation defining the basic API. It refers to the elements as ‘left’ and ‘right’. It also implements the Map.Entry interface where the key is ‘left’ and the value is ‘right’.

Subclass implementations may be mutable or immutable. However, there is no restriction on the type of the stored objects that may be stored. If mutable objects are stored in the pair, then the pair itself effectively becomes mutable.

Parameters:
  • <L> – The left element type.

  • <R> – The right element type.

Since:

3.0

Constructors

Pair()

Constructs a new instance.

public Pair()[source]

Constructs a new instance.

Fields and constants

EMPTY_ARRAY

An empty array.

public static final Pair<?, ?>[] EMPTY_ARRAY[source]

An empty array.

Consider using #emptyArray() to avoid generics warnings.

Since:

3.10

Methods

accept(FailableBiConsumer)

Accepts this key and value as arguments to the given consumer.

apply(FailableBiFunction)

Applies this key and value as arguments to the given function.

compareTo(Pair)

Compares the pair based on the left element followed by the right element.

emptyArray()

Returns the empty array singleton that can be assigned without compiler warning.

equals(Object)

Compares this pair to another based on the two elements.

getKey()

Gets the key from this pair.

getLeft()

Gets the left element from this pair.

getRight()

Gets the right element from this pair.

getValue()

Gets the value from this pair.

hashCode()

Returns a suitable hash code.

of(Object, Object)

Creates an immutable pair of two objects inferring the generic types.

of(Entry)

Creates an immutable pair from a map entry.

ofNonNull(Object, Object)

Creates an immutable pair of two non-null objects inferring the generic types.

toString()

Returns a String representation of this pair using the format (left,right).

toString(String)

Formats the receiver using the given format.

public <E extends Throwable> void accept(FailableBiConsumer<L, R, E> consumer) throws E[source]

Accepts this key and value as arguments to the given consumer.

Type parameters:

<E> – The kind of thrown exception or error.

Parameters:

consumer – The consumer to call.

Throws:

E – Thrown when the consumer fails.

Since:

3.13.0

public <V, E extends Throwable> V apply(FailableBiFunction<L, R, V, E> function) throws E[source]

Applies this key and value as arguments to the given function.

Type parameters:
  • <V> – The function return type.

  • <E> – The kind of thrown exception or error.

Parameters:

function – The consumer to call.

Returns:

The function’s return value.

Throws:

E – Thrown when the consumer fails.

Since:

3.13.0

@Override public int compareTo(Pair<L, R> other)[source]

Compares the pair based on the left element followed by the right element. The types must be Comparable.

Parameters:

other – The other pair, not null.

Returns:

negative if this is less, zero if equal, positive if greater.

@SuppressWarnings({"unchecked"}) public static <L, R> Pair<L, R>[] emptyArray()[source]

Returns the empty array singleton that can be assigned without compiler warning.

Type parameters:
  • <L> – The left element type.

  • <R> – The right element type.

Returns:

The empty array singleton that can be assigned without compiler warning.

Since:

3.10

@Override public boolean equals(Object obj)[source]

Compares this pair to another based on the two elements.

Parameters:

obj – The object to compare to, null returns false.

Returns:

true if the elements of the pair are equal.

@Override public final L getKey()[source]

Gets the key from this pair.

This method implements the Map.Entry interface returning the left element as the key.

Returns:

The left element as the key, may be null.

public abstract L getLeft()[source]

Gets the left element from this pair.

When treated as a key-value pair, this is the key.

Returns:

The left element, may be null.

public abstract R getRight()[source]

Gets the right element from this pair.

When treated as a key-value pair, this is the value.

Returns:

The right element, may be null.

@Override public R getValue()[source]

Gets the value from this pair.

This method implements the Map.Entry interface returning the right element as the value.

Returns:

The right element as the value, may be null.

@Override public int hashCode()[source]

Returns a suitable hash code.

The hash code follows the definition in Map.Entry.

Returns:

The hash code.

public static <L, R> Pair<L, R> of(L left, R right)[source]

Creates an immutable pair of two objects inferring the generic types.

Type parameters:
  • <L> – The left element type.

  • <R> – The right element type.

Parameters:
  • left – The left element, may be null.

  • right – The right element, may be null.

Returns:

An immutable pair formed from the two parameters, not null.

public static <L, R> Pair<L, R> of(Map.Entry<L, R> pair)[source]

Creates an immutable pair from a map entry.

Type parameters:
  • <L> – The left element type.

  • <R> – The right element type.

Parameters:

pair – The map entry.

Returns:

An immutable pair formed from the map entry.

Since:

3.10

public static <L, R> Pair<L, R> ofNonNull(L left, R right)[source]

Creates an immutable pair of two non-null objects inferring the generic types.

Type parameters:
  • <L> – The left element type.

  • <R> – The right element type.

Parameters:
  • left – The left element, may not be null.

  • right – The right element, may not be null.

Returns:

An immutable pair formed from the two parameters, not null.

Throws:

java.lang.NullPointerException – Thrown if any input is null.

Since:

3.13.0

@Override public String toString()[source]

Returns a String representation of this pair using the format (left,right).

Returns:

A string describing this object, not null.

public String toString(String format)[source]

Formats the receiver using the given format.

This uses String#format(String, Object...) to the format. Two variables may be used to embed the left and right elements. Use %1$s for the left element (key) and %2$s for the right element (value).

Parameters:

format – The format string, optionally containing %1$s and %2$s, not null.

Returns:

The formatted string, not null.

See also:

String#format(String, Object...)