On this page

ImmutablePair

public class org.apache.commons.lang3.tuple.ImmutablePair<L, R> extends Pair<L, R>[source]

An immutable pair consisting of two Object elements.

Although the implementation is immutable, there is no restriction on the objects that may be stored. If mutable objects are stored in the pair, then the pair itself effectively becomes mutable.

#ThreadSafe# if both paired objects are thread-safe

Parameters:
  • <L> – The left element type

  • <R> – The right element type

Since:

3.0

Constructors

ImmutablePair(Object, Object)

Create a new pair instance.

public ImmutablePair(L left, R right)[source]

Create a new pair instance.

Parameters:
  • left – The left value, may be null

  • right – The right value, may be null

Fields and constants

EMPTY_ARRAY

An empty array.

left

Left object

right

Right object

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

An empty array.

Consider using #emptyArray() to avoid generics warnings.

Since:

3.10

public final L left[source]

Left object

public final R right[source]

Right object

Methods

emptyArray()

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

getLeft()

Gets the left element from this pair.

getRight()

Gets the right element from this pair.

left(Object)

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

nullPair()

Returns an immutable pair of nulls.

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.

right(Object)

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

setValue(Object)

Sets no value and always throws UnsupportedOperationException.

@SuppressWarnings({"unchecked"}) public static <L, R> ImmutablePair<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 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.

@Override public 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.

public static <L, R> Pair<L, R> left(L left)[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.

Returns:

An immutable formed from the two parameters, not null.

Since:

3.11

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

Returns an immutable pair of nulls.

Type parameters:
  • <L> – The left element of this pair. Value is null.

  • <R> – The right element of this pair. Value is null.

Returns:

An immutable pair of nulls.

Since:

3.6

public static <L, R> ImmutablePair<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 formed from the two parameters, not null.

public static <L, R> ImmutablePair<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 existing map entry.

Returns:

An immutable formed from the map entry.

Since:

3.10

public static <L, R> ImmutablePair<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 formed from the two parameters, not null.

Throws:

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

Since:

3.13.0

public static <L, R> Pair<L, R> right(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:

right – The right element, may be null.

Returns:

An immutable formed from the two parameters, not null.

Since:

3.11

@Override public R setValue(R value)[source]

Sets no value and always throws UnsupportedOperationException.

This pair is immutable, so this operation is not supported.

Parameters:

value – The value to set

Returns:

never

Throws:

java.lang.UnsupportedOperationException – Thrown because this operation is not supported.