Spiderman Spiderman - 1 year ago 69
Java Question

Java - How to create new Entry (key, value)

I'd like to create new item that similarly to

that will contain the structure

The problem is that I can't instantiate a
because it's an interface.

Does anyone know how to create a new generic key/value object for Map.Entry?

Answer Source

You can just implement the Map.Entry<K, V> interface yourself:

import java.util.Map;

final class MyEntry<K, V> implements Map.Entry<K, V> {
    private final K key;
    private V value;

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

    public K getKey() {
        return key;

    public V getValue() {
        return value;

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

And then use it:

Map.Entry<String, Object> entry = new MyEntry<String, Object>("Hello", 123);