[java] A KeyValuePair in Java

I'm looking for a KeyValuePair class in Java.
Since java.util heavily uses interfaces there is no concrete implementation provided, only the Map.Entry interface.

Is there some canonical implementation I can import? It is one of those "plumbers programming" classes I hate to implement 100x times.

This question is related to java key-value

The answer is


The class AbstractMap.SimpleEntry is generic and can be useful.


Android programmers could use BasicNameValuePair

Update:

BasicNameValuePair is now deprecated (API 22). Use Pair instead.

Example usage:

Pair<Integer, String> simplePair = new Pair<>(42, "Second");
Integer first = simplePair.first; // 42
String second = simplePair.second; // "Second"

Hashtable<String, Object>

It is better than java.util.Properties which is by fact an extension of Hashtable<Object, Object>.


The Pair class from Commons Lang might help:

Pair<String, String> keyValue = new ImmutablePair("key", "value");

Of course, you would need to include commons-lang.


import java.util.Map;

public class KeyValue<K, V> implements Map.Entry<K, V>
{
    private K key;
    private V value;

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

    public K getKey()
    {
        return this.key;
    }

    public V getValue()
    {
        return this.value;
    }

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

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

You can create your custom KeyValuePair class easily

public class Key<K, V>{

    K key;
    V value;

    public Key() {

    }

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

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

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

}

I like to use

Properties

Example:

Properties props = new Properties();

props.setProperty("displayName", "Jim Wilson"); // (key, value)

String name = props.getProperty("displayName"); // => Jim Wilson

String acctNum = props.getProperty("accountNumber"); // => null

String nextPosition = props.getProperty("position", "1"); // => 1

If you are familiar with a hash table you will be pretty familiar with this already


My favorite is

HashMap<Type1, Type2>

All you have to do is specify the datatype for the key for Type1 and the datatype for the value for Type2. It's the most common key-value object I've seen in Java.

https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html


I've published a NameValuePair class in GlobalMentor's core library, available in Maven. This is an ongoing project with a long history, so please submit any request for changes or improvements.


Use of javafx.util.Pair is sufficient for most simple Key-Value pairings of any two types that can be instantiated.

Pair<Integer, String> myPair = new Pair<>(7, "Seven");
Integer key = myPair.getKey();
String value = myPair.getValue();