A KeyValuePair in Java

JavaKey Value

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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"

Solution 3 - Java

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.

Solution 4 - Java

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();

Solution 5 - Java

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;
    }
}

Solution 6 - Java

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

Solution 7 - Java

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;
    }
    
}

Solution 8 - Java

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

Solution 9 - Java

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.

Solution 10 - Java

Hashtable<String, Object>

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmaayankView Question on Stackoverflow
Solution 1 - JavaEyal SchneiderView Answer on Stackoverflow
Solution 2 - JavakrekerView Answer on Stackoverflow
Solution 3 - JavaremipodView Answer on Stackoverflow
Solution 4 - JavaaaroncarsonartView Answer on Stackoverflow
Solution 5 - JavaNeoheuristView Answer on Stackoverflow
Solution 6 - JavaNathan Clark BaumgartnerView Answer on Stackoverflow
Solution 7 - JavaMohhamed NabilView Answer on Stackoverflow
Solution 8 - Javamatthewpark319View Answer on Stackoverflow
Solution 9 - JavaGarret WilsonView Answer on Stackoverflow
Solution 10 - JavaOleg MikhailovView Answer on Stackoverflow