Introducing pair in Java

describes Pair

in Java
In this article, we discussed a very useful programming concept called Pair. Pairing provides a convenient way to handle simple key-value associations, especially useful when we want to return two values from a method.

is available in the core Java library using the implementation of Pair. In addition, some third-party libraries, such as Apache Commons and Vavr, have exposed this functionality in their respective apis.

core Java pairing implementation

Pair class
In the javafx.util package, the class constructor takes two arguments, the key and the corresponding value:

Pair class

    Pair<Integer, String> pair = new Pair<>(1, "One");
    Integer key = pair.getKey();
    String value = pair.getValue();

The

example describes a simple Integer to String mapping using the Pair class. In the example, the getKey method returns the key object, and the getValue method returns the corresponding value object.

AbstractMap. SimpleEntry and AbstractMap. SimpleImmutableEntry

SimpleEntry is defined in the abstract class AbstractMap and is constructed in a similar way to Pair:

    AbstractMap.SimpleEntry<Integer, String> entry 
      = new AbstractMap.SimpleEntry<>(1, "one");
    Integer key = entry.getKey();
    String value = entry.getValue();

has keys and values that can be obtained using standard getter and setter methods.

The AbstractMap class also contains a nested class that represents an immutable pairing: the SimpleImmutableEntry class.

    AbstractMap.SimpleImmutableEntry<Integer, String> entry
      = new AbstractMap.SimpleImmutableEntry<>(1, "one");

applications with variable pairing, in addition to the configuration value cannot be modified, try to modify will throw an UnsupportedOperationException anomalies.

Apache Commons

the Apache Commons library, org.apache.com mons. Lang3. The tuple package provides Pair abstract class, cannot be instantiated directly.
has two subclasses, which represent mutable and immutable pairs: ImmutablePair and MutablePair. Both implement access to key/value and setter and getter methods:

    ImmutablePair<Integer, String> pair = new ImmutablePair<>(2, "Two");
    Integer key = pair.getKey();
    String value = pair.getValue();

try in ImmutablePair setValue method, will throw an UnsupportedOperationException anomalies. But execution on variable pairings is perfectly normal:

    Pair<Integer, String> pair = new MutablePair<>(3, "Three");
    pair.setValue("New Three");

Vavr library
The immutable Tuple2 class in the

Vavr library provides pairing:

    Tuple2<Integer, String> pair = new Tuple2<>(4, "Four");
    Integer key = pair._1();
    String value = pair._2();

in this implementation, an object cannot be modified after it has been created, so the update method returns a new instance after the change:

    tuplePair = pair.update2("New Four");

summary

this article discusses the Java pairing concept and shows examples of its different implementations, core Java and third-party provided implementations.

Read More: