create map.entry java code example

Example: how to instanciate map.entry java

/**
 * @author Jack Waller <[email protected]>
 * @version 1.1
 * @since 2020-03-28
 * A implementation of Map.Entry.
 */
public final class Pair<K, V> implements Map.Entry<K, V> {

    //variables
    private final K key;
    private V value;

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

    //methods

    /**
     * Returns the key.
     * @return K
     */
    @Override
    public K getKey() {
        return key;
    }

    /**
     * Returns the value.
     * @return V
     */
    @Override
    public V getValue() {
        return value;
    }

    /**
     * Sets the value, returns the old value
     * @param v value to set
     * @return V
     */
    @Override
    public V setValue(V v) {
        V old = this.value;
        this.value = v;
        return old;
    }
}

Tags:

Java Example