🌐
GeeksforGeeks
geeksforgeeks.org › java › map-put-method-in-java-with-examples
Map put() Method in Java with Examples - GeeksforGeeks
July 11, 2025 - The Map put() method associates the specified value with the specified key in the map. The map put() method is used to insert new key-value pairs inside a map in Java.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Map.html
Map (Java Platform SE 8 )
July 21, 2026 - Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map.
🌐
GeeksforGeeks
geeksforgeeks.org › java › hashmap-put-method-in-java
Java HashMap put() Method - GeeksforGeeks
July 11, 2025 - The put() method of the Java HashMap class is used to add or update the key-value pairs in the map.
🌐
W3Schools
w3schools.com › JAVA › ref_hashmap_put.asp
Java HashMap put() Method
The put() method writes an entry into the map. If an entry with the same key already exists then the value of that entry will be changed.
🌐
Scaler
scaler.com › home › topics › java map.put()
Java Map.put() - Scaler Topics
April 14, 2024 - As you can see, when an existing ... By doing this we are replacing the old value with the new value. Java HashMap put() is a method that is used to add key/value pairs inside a hashmap....
🌐
GeeksforGeeks
geeksforgeeks.org › java › map-interface-in-java
Map Interface in Java - GeeksforGeeks
Explanation: The put() method is used with an existing key to update its value. Here, the value of key 2 changes from "Geeks" to "For". To remove an element from the Map, we can use the remove() method.
Published: August 31, 2026
🌐
TutorialsPoint
tutorialspoint.com › java › util › hashmap_put.htm
Java HashMap put() Method
The Java HashMap put(K key, V value) method is used to associate the specified value with the specified key in this map.
🌐
Vultr
docs.vultr.com › java › standard library › java › util › hashmap › put()
Java HashMap put() - Insert Key-Value Pair
September 27, 2024 - Use the put() method to add a key and value. ... import java.util.HashMap; HashMap<String, Integer> map = new HashMap<>(); map.put("Apple", 10); map.put("Orange", 20);
🌐
Jenkov
jenkov.com › tutorials › java-collections › map.html
Java Map
For more information about Java Generics, see the Java Generics Tutorial. To add elements to a Map you call its put() method.
Find elsewhere
🌐
Codecademy
codecademy.com › docs › java › hashmap › .put()
Java | HashMap | .put() | Codecademy
May 31, 2025 - In Java, the .put() method adds a key-value pair to a HashMap; if the key already exists, it updates the corresponding value.
🌐
JavaGoal
javagoal.com › home › how to add an object in hashmap by hashmap put() method
java hashmap put and java map put all example - JavaGoal
September 10, 2020 - Where, map is specified Map which you want to copy. return Void, it doesn’t return anything. import java.util.HashMap; public class ExampleOfHashMap { public static void main(String[] args) { HashMap<Integer, String> hashMap = new HashMap<Integer, String>(); hashMap.put(1, "JavaGoal.com"); hashMap.put(2, "Learning"); hashMap.put(3, "Website"); HashMap<Integer, String> newHashMap = new HashMap<Integer, String>(); newHashMap.putAll(hashMap); System.out.println("All Pair of HashMap: "+newHashMap); } }
🌐
Medium
medium.com › javarevisited › maps-are-an-important-data-structure-in-java-that-store-key-value-pairs-8fba84fe8a1f
Basic Operations on Maps in Java: Adding, Removing, and Accessing Elements | by Piyu Jain | Javarevisited | Medium
October 31, 2023 - In this article, we will discuss the basic operations of maps, including adding, removing, and accessing elements in Java. Adding Elements to a Map To add an element to a Map, we need to use the put() method. The put() method takes two parameters: ...
🌐
Medium
medium.com › @avuzia › under-the-bonnet-a-step-by-step-look-at-javas-hashmap-put-75360a8c707e
Under the Bonnet: A Step-by-Step Look at Java’s HashMap.put() | by Edhar Avuzi | Medium
July 28, 2025 - This is where the main logic resides. putVal is responsible for initialising the map, placing the new entry, and handling any potential collisions.
🌐
Educative
educative.io › answers › what-is-put-in-java-hashmap
What is put in Java HashMap?
The put function is used to insert a new key-value pair into a HashMap. We insert a key and its corresponding value into the map. If the key already exists, its old value is replaced with a new one. java.util.HashMap.put(K key, V value) In the ...
🌐
Deep Java
digibeatrix.com › home › java collections › java map.put: add, replace, and safely handle previous values
Java Map.put: Add, Replace, and Safely Handle Previous Values - Deep Java
August 10, 2026 - Use putIfAbsent, merge, or compute when their update policy matches the requirement. ... Map.put returns the value previously associated with the key, or null when there was no mapping for that key.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-util-hashmap-in-java-with-examples
HashMap in Java - GeeksforGeeks
Explanation: Uses put() with an existing key to replace its current value with a new value. To remove an element from the Map, we can use the remove() method. It removes the mapping for the specified key if it is present and returns the value ...
Published: August 31, 2026
Top answer
1 of 3
7

I assume you mean Map.put - because there is no Map.add. But even if they had the same name - these methods behave differently enough to render any attempt to unify their interface meaningless.

Look at the way Collection.add's return value's doc is worded:

true if this collection changed as a result of the call

Now, the only valid reason for the collection to not change is if it's a set and the item is already in there(any other reason should throw), but that wording was chosen because it makes sense for all collections, without having to make special clauses for sets and not-sets.

But does it make sense for maps? Consider this:

HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(1, 2);
map.put(2, 2);

/*case 1*/ map.put(1, 1); // value changed - would have returned `true`
/*case 2*/ map.put(2, 2); // nothing changed - would have returned `false`
/*case 3*/ map.put(3, 3); // key-value pair added - would have returned `true`

Case 1 and 3 both return true, but they are very different - one is overriding an entry and the other adding a new one. So maybe case 1 should have returned false? But then it would have been the same as case 2, even though one is changing the map and the other doesn't...

So, copying Collection.add's behavior to Map.put makes no sense. How about copying Map.put's behavior to Collection.add? What if we had Collection.add return the old value?

I see several problems with that:

  1. It makes the interface more complicated - now it needs to give different definitions for maps and sets, or make a more complex definition(e.g. - "if the item was not added because it was already there, return the item that was already there". Ugh...)
  2. It's not very useful. You already have the item that's already in the collection - you just sent it as an argument to add!
  3. What if we have a TreeSet with a custom comparator?

    TreeSet<Integer> treeSet = new TreeSet<Integer>((a, b) -> Integer.compare(a % 10, b % 10));
    
    treeSet.add(11);
    treeSet.add(21); // should this return `11` or `21`?
    

    This is not a problem with Map, because it's looking by the key, and is already forced to define what you get when you request for that key.

  4. It forces the collection to actually retrieve the value. What if the Collection implementation is a bloom filter? It now has to actually look for the object instead of doing a quick hash match. What if it's a wrapper around a database table? It now has to create an object that you already have!

    Of course, Map would have a similar problem - but at least there the old value is important, because you don't know what it is and it's going to disappear. But with Collection? It's all cost and no gain...

2 of 3
2

Collection.add returns whether the collection has changed or not.

Map.put returns the previous element for the key.

These are simply different functionality.

There is not "equivalent" method in Collection interface. However you have List.set which returns the previous element in the position. Note that a Map is not a Collection.