The Pair Class Implementation in Java


The Java Language does not have a inbuilt Pair class to hold the key-value pairs, thus here is a handy Pair Implementation that allows you to store key-value pair in a class – which is serializable.

package helloacm.com

import java.io.Serializable;

/**
 * <p>A convenience class to represent name-value pairs.</p>
 *
 * @since JavaFX 2.0
 */
public class Pair<K, V> implements Serializable {

  private static final long serialVersionUID = -111223421881811738599L;

  /**
   * Key of this <code>Pair</code>.
   */
  private K key;
  /**
   * Value of this this <code>Pair</code>.
   */
  private V value;

  /**
   * Creates a new pair
   *
   * @param key The key for this pair
   * @param value The value to use for this pair
   */
  public Pair(@NamedArg("key") K key, @NamedArg("value") V value) {
    this.key = key;
    this.value = value;
  }

  /**
   * Gets the value for this pair.
   *
   * @return value for this pair
   */
  public V getValue() {
    return value;
  }

  /**
   * Gets the key for this pair.
   *
   * @return key for this pair
   */
  public K getKey() {
    return key;
  }

  /**
   * <p><code>String</code> representation of this <code>Pair</code>.</p>
   *
   * <p>The default name/value delimiter '=' is always used.</p>
   *
   * @return <code>String</code> representation of this <code>Pair</code>
   */
  @Override
  public String toString() {
    return key + "=" + value;
  }

  /**
   * <p>Generate a hash code for this <code>Pair</code>.</p>
   *
   * <p>The hash code is calculated using both the name and the value of the <code>Pair</code>.</p>
   *
   * @return hash code for this <code>Pair</code>
   */
  @Override
  public int hashCode() {
    // name's hashCode is multiplied by an arbitrary prime number (65537)
    // in order to make sure there is a difference in the hashCode between
    // these two parameters:
    //  name: a  value: aa
    //  name: aa value: a
    return key.hashCode() * 65537 + (value == null ? 0 : value.hashCode());
  }

  /**
   * <p>Test this <code>Pair</code> for equality with another <code>Object</code>.</p>
   *
   * <p>If the <code>Object</code> to be tested is not a <code>Pair</code> or is <code>null</code>,
   * then this method returns <code>false</code>.</p>
   *
   * <p>Two <code>Pair</code>s are considered equal if and only if both the names and values are
   * equal.</p>
   *
   * @param o the <code>Object</code> to test for equality with this <code>Pair</code>
   * @return <code>true</code> if the given <code>Object</code> is equal to this <code>Pair</code>
   * else <code>false</code>
   */
  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o instanceof Pair) {
      var pair = (Pair) o;
      return !((key != null ? !key.equals(pair.key) : pair.key != null) || (value != null ? !value
          .equals(pair.value) : pair.value != null));
    }
    return false; // inequal if it is different type
  }
}

Same usage:

var data = new Pair<String, String>("Key", "Value");
System.out.println(data.getKey()); // Key
System.out.println(data.getValue()); // Value
System.out.println(data); // Key=Value

–EOF (The Ultimate Computing & Technology Blog) —

626 words
Last Post: Teaching Kids Programming - Breadth First Search Algorithm to Check If Two Binary Trees are Same
Next Post: Teaching Kids Programming - Number of Changing Directions

The Permanent URL is: The Pair Class Implementation in Java (AMP Version)

Leave a Reply