All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dongliu.commons.collection.Pair Maven / Gradle / Ivy

The newest version!
package net.dongliu.commons.collection;

import java.io.Serializable;

/**
 * mutable pair with first, and second, or key and value
 *
 * @author Liu Dong
 */
public class Pair implements Serializable {
    private static final long serialVersionUID = 5218577943220806024L;
    private K key;
    private V value;

    public Pair() {
    }

    public Pair(K key, V value) {
        this.key = key;
        this.value = value;
    }

    public static  Pair of(K key, V value) {
        return new Pair<>(key, value);
    }

    public K getKey() {
        return key;
    }

    public void setKey(K key) {
        this.key = key;
    }

    public V getValue() {
        return value;
    }

    public void setValue(V value) {
        this.value = value;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Pair pair = (Pair) o;

        if (key != null ? !key.equals(pair.key) : pair.key != null) return false;
        if (value != null ? !value.equals(pair.value) : pair.value != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = key != null ? key.hashCode() : 0;
        result = 31 * result + (value != null ? value.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "(key" + " -> " + "value)";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy