How to put an Entry into a Map?

java map
java map add value to existing key
java map.entry implementation
java map compute
hashmap java
hashmap put() method implementation in java
java make a map entry
put multiple values in hashmap java

Is there any way that I can put a whole Entry object to a Map object like:

map.put(entry);

instead of passing a key-value pair like:

map.put(key,value);

I have searched on the Map interface methods but there is no method that takes an entry and puts it in the map. Therefore I have implemented it by myself using a little bit of inheritance and Java 8 interfaces.

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Maps {

    // Test method
    public static void main(String[] args) {
        Map.Entry<String, String> entry1 = newEntry("Key1", "Value1");
        Map.Entry<String, String> entry2 = newEntry("Key2", "Value2");

        System.out.println("HashMap");
        MyMap<String, String> hashMap = new MyHashMap<>();
        hashMap.put(entry1);
        hashMap.put(entry2);

        for (String key : hashMap.keySet()) {
            System.out.println(key + " = " + hashMap.get(key));
        }

        System.out.println("\nTreeMap");
        MyMap<String, String> treeMap = new MyTreeMap<>();
        treeMap.put(entry1);
        treeMap.put(entry2);


        for (String key : treeMap.keySet()) {
            System.out.println(key + " = " + treeMap.get(key));
        }
    }


    /**
     * Creates a new Entry object given a key-value pair.
     * This is just a helper method for concisely creating a new Entry.
     * @param key   key of the entry
     * @param value value of the entry
     * 
     * @return  the Entry object containing the given key-value pair
     */
    private static <K,V> Map.Entry<K,V> newEntry(K key, V value) {
        return new AbstractMap.SimpleEntry<>(key, value);
    }

    /**
     * An enhanced Map interface.
     */
    public static interface MyMap<K,V> extends Map<K,V> {

        /**
         * Puts a whole entry containing a key-value pair to the map.
         * @param entry 
         */
        public default V put(Entry<K,V> entry) {
            return put(entry.getKey(), entry.getValue());
        }
    }

    /**
     * An enhanced HashMap class.
     */
    public static class MyHashMap<K,V> extends HashMap<K,V> implements MyMap<K,V> {}

    /**
     * An enhanced TreeMap class.
     */
    public static class MyTreeMap<K,V> extends TreeMap<K,V> implements MyMap<K,V> {}
}

The MyMap interface is just an interface that extends the Map interface by adding one more method, the public default V put(Entry<K,V> entry). Apart from just defining the method, a default implementation is coded too. Doing that, we can now add this method to any class that implements the Map interface just by defining a new class that implements the MyMap interface and extending the map implementation class of our choice. All of that in one line! This is demonstrated in the bottom of the code above where two classes are created each extending the HashMap and the TreeMap implementations.

HashMap put() Method in Java, HashMap.put() method of HashMap is used to insert a mapping into a map. This means we can insert a specific key and the value it is mapping to  An alternative way to insert elements in a map is by using member function map::operator []. Internally, map containers keep all their elements sorted by their key following the criterion specified by its comparison object. The elements are always inserted in its respective position following this ordering. The parameters determine how many


To instantiate a Map with Entries (in the same way as you can do Arrays.asList(T... a) or Sets.newHashSet(T... a) in Google Guava library, I found this:

import java.util.AbstractMap;
import java.util.Map;

public class MapWithEntries {

    private static final Map.Entry<String, String> ENTRY_1 = new AbstractMap.SimpleEntry<>("A", "Hello");
    private static final Map.Entry<String, String> ENTRY_2 = new AbstractMap.SimpleEntry<>("B", "World");

    private static final Map<String, String> MAP_WITH_ENTRIES = Map.ofEntries(ENTRY_1, ENTRY_2);
}

Map.Entry interface in Java with example, Set<Map.Entry> entrySet() – Returns the Set view of the entire map. Note : This is not a method of Map.entry interface but it is discussed here because this  How to pin a pile of addresses onto a Google map Taking a huge list of names, addresses, phone numbers and email addresses and turning it into a Google map can be very useful and surprisingly easy.


Guava's ImmutableMap.Builder takes entries directly.

Obviously that doesn't directly modify an existing Map, but it might well scratch the same itch.

Java Map, It is possible to replace an element in a Java Map map.put("key", "val1"); //now contains "key" entry  The java.util.HashMap.put() method of HashMap is used to insert a mapping into a map. This means we can insert a specific key and the value it is mapping to into a particular map. If an existing key is passed then the previous value gets replaced by the new value. If a new pair is passed, then the pair gets inserted as a whole.


Solution if you're using Java Platform SE 8:

SimpleEntry and SimpleImmutableEntry classes implement interface Map.Entry

import java.util.AbstractMap.SimpleEntry;
import java.util.AbstractMap.SimpleImmutableEntry;

List<Map.Entry<K, V>> map = new ArrayList<>(); 
map.add(new SimpleEntry<K,V>(key,value));
map.add(new SimpleImmutableEntry<K,V>(key,value)); // constructs immutable entries for thread-safe operation

Map (Java Platform SE 8 ), Returns a Set view of the mappings contained in this map. Performs the given action for each entry in this map until all entries have been processed or the  But when put() returns null, we can not be sure that that it has added a new entry in the map or there was an existing key in the map whose value was null. To improve over this HashMap provides two overloaded versions of replace() member function.


The Map Interface (The Java™ Tutorials > Collections > Interfaces), Entry , the type of the elements in this Set . The Collection views provide the only means to iterate over a Map . This example illustrates the standard idiom for  The point is that the first line of your codes is not what you expected. You should use: val map = scala.collection.mutable.Map[A,B]() You then have multiple equivalent alternatives to add items:


Java - The Map.Entry Interface, Each of these set elements is a Map.Entry object. Following table summarizes the methods declared by this interface −. Sr.No. Method & Description. 1. What is the best way to convert a Map<key,value> to a List<value>? Just iterate over all values and insert them in a list or am I overlooking something? I think it's not a good model if one has to convert map into list. One should write the code in such manner that this situation doesn't arrise. – Bugs Happen Dec 5 '17 at 17:13.


java.util.HashMap.put() Method, java.util.HashMap.put() Method - The put(K key, V value) method is used to associate the specified value with the specified key in this map. Find local businesses, view maps and get driving directions in Google Maps.