Timeline for answer to Java map.get(key) - Is there a way to automatically call put(key) and return if the key doesn't exist? by Roger Lindsjö
Current License: CC BY-SA 4.0
Post Revisions
11 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Mar 6, 2022 at 13:54 | history | edited | Roger Lindsjö | CC BY-SA 4.0 |
Clarified what is returned
|
| Dec 4, 2016 at 11:12 | history | edited | Roger Lindsjö | CC BY-SA 3.0 |
Java 8 standard and lazy methods
|
| Nov 21, 2016 at 15:50 | comment | added | Roger Lindsjö |
You probably want computeIfAbsent as shown by @itake
|
|
| Nov 21, 2016 at 12:15 | comment | added | efinal | it doesn't work is the value itself is supposed to be a collection. as what I wanted is add an empty collection AND RETURN IT so that I can add items into that collection. | |
| Nov 13, 2015 at 4:12 | comment | added | yelliver | java.util.Map has putIfAbsent from JDK 8, not only ConcurrentMap. | |
| Jul 11, 2014 at 12:47 | history | edited | Roger Lindsjö | CC BY-SA 3.0 |
Fix description from comments.
|
| Jul 5, 2014 at 1:47 | comment | added | Saad Malik | @user802421 is right--this will return null if the value is inserted. | |
| Apr 4, 2013 at 0:09 | vote | accept | Sridhar Sarnobat | ||
| Dec 1, 2011 at 21:45 | comment | added | Christian Semrau |
Note that this forces you to create a new object, even if there is already one available in the map. If you really only create a new Object(), this may be negligible.
|
|
| Nov 30, 2011 at 23:31 | comment | added | user802421 | Returns: the previous value associated with the specified key, or null if there was no mapping for the key | |
| Nov 30, 2011 at 22:48 | history | answered | Roger Lindsjö | CC BY-SA 3.0 |