Java Pair

Introduction

In Java, a Pair is a simple data structure that allows you to store two values together as a single unit. It is commonly used when you need to return multiple values from a method or when you want to associate two values together. The Pair class is part of the javafx.util package, which provides a collection of utility classes for JavaFX.

In this article, we will explore the Pair class and how to use it in Java applications. We will cover its basic usage, methods, and provide code examples to demonstrate its functionality.

Basic Usage

To use the Pair class, you need to import it from the javafx.util package:

import javafx.util.Pair;

Once imported, you can create a Pair object by providing values for the two elements:

Pair<String, Integer> pair = new Pair<>("John", 25);

In this example, we have created a Pair object with a String element "John" and an Integer element 25.

Methods

The Pair class provides several useful methods to access and manipulate the elements:

  • getKey() returns the key element of the pair.
  • getValue() returns the value element of the pair.
  • setKey(K key) sets a new key element for the pair.
  • setValue(V value) sets a new value element for the pair.
  • equals(Object obj) checks if the given object is equal to the pair.
  • hashCode() returns the hash code value for the pair.

Let's explore these methods with some code examples:

import javafx.util.Pair;

public class PairExample {
    public static void main(String[] args) {
        Pair<String, Integer> pair = new Pair<>("John", 25);
        
        System.out.println("Key: " + pair.getKey()); // Output: Key: John
        System.out.println("Value: " + pair.getValue()); // Output: Value: 25
        
        pair.setKey("Jane");
        pair.setValue(30);
        
        System.out.println("Updated Key: " + pair.getKey()); // Output: Updated Key: Jane
        System.out.println("Updated Value: " + pair.getValue()); // Output: Updated Value: 30
        
        Pair<String, Integer> anotherPair = new Pair<>("John", 25);
        
        System.out.println("Is pair equal to anotherPair? " + pair.equals(anotherPair)); // Output: Is pair equal to anotherPair? false
        
        System.out.println("Pair hash code: " + pair.hashCode()); // Output: Pair hash code: 961
    }
}

In this example, we create a Pair object with the key "John" and the value 25. We then use the getKey() and getValue() methods to retrieve the elements. Next, we update the key to "Jane" and the value to 30 using the setKey() and setValue() methods. We create another Pair object with the same values and use the equals() method to check if the pairs are equal. Finally, we use the hashCode() method to get the hash code value of the pair.

Use Cases

The Pair class can be used in various scenarios where you need to associate two values together. Some common use cases include:

Returning multiple values from a method

In Java, a method can only return a single value. However, by using a Pair object, you can return multiple values as a single unit. For example:

import javafx.util.Pair;

public class Calculator {
    public static Pair<Integer, Integer> divide(int dividend, int divisor) {
        int quotient = dividend / divisor;
        int remainder = dividend % divisor;
        return new Pair<>(quotient, remainder);
    }
    
    public static void main(String[] args) {
        Pair<Integer, Integer> result = divide(10, 3);
        System.out.println("Quotient: " + result.getKey()); // Output: Quotient: 3
        System.out.println("Remainder: " + result.getValue()); // Output: Remainder: 1
    }
}

In this example, the divide() method returns both the quotient and remainder of a division operation as a Pair object. The caller can then use the getKey() and getValue() methods to retrieve the individual values.

Associating two related values

Sometimes, you may need to associate two related values together. For example, you might want to store a person's name and age together. The Pair class provides a convenient way to do this:

import javafx.util.Pair;

public class Person {
    private Pair<String, Integer> nameAndAge;
    
    public Person(String name, int age) {
        nameAndAge = new Pair<>(name, age);
    }
    
    public String getName() {
        return nameAndAge.getKey();
    }
    
    public int getAge() {
        return nameAndAge.getValue();
    }
    
    public static void main(String[] args) {
        Person person = new Person("John", 25);
        System.out.println("Name: " + person.getName()); // Output: Name: John
        System.out