I have this code:
List<Map<Object, Object>> list = new ArrayList<>();
list.add(new HashMap<>().put("key", "value"));
However it tells me this:
add (java.util.Map) in List cannot be applied to (java.lang.Object)
Why can't add
function of list read the Map
instance as an Object
instance? But if it's a simple list it
List<Object> simpleList = new
simpleList.add("
simpleList.add(new HashMap<>().put("sample","sample"));
Edit :
This also does not work:
Map<String, String> map = new HashMap<>();
map.put("key", "value");
list.add(map);
add(java.util.Map) in List cannot be applied to (java.util.Map)
Edit 2 :
Thanks for your answers I understand now why it won't work. So what would be the best possible way to implement this? It is iterating in a loop because I'm adding multiple HashMaps to a List.
Map<Object, Object> map = new HashMap<>();
something.foreach{ thing ->{
if(map!=null)
map.clear();
map.put("key", thing);
list.add(map);
}
}
Is map
mutable? Or is this bad code?
HashMap.put()
returns just the value as an object so doing new HashMap<>().put("key", "value");
does not return the hashmap but the value instead or null;
put Returns:the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key.)
You need to do
Map<Object, Object> item = new HashMap<>();
item.put("key", "value");
list.add(item);
The put()
method within HashMap
returns the value previously inserted in the map and not the object itself.
Edit: You said that this doesn't work:
Map<String, String> map = new HashMap<>();
map.put("key", "value");
list.add(map);
That's because your list contains elements of type "Map of objects" and you're trying to insert a "Map of Strings"
You are not putting an instance of java.util.Map<java.lang.Object,java.lang.Object>
but rather the result of Hasmap.put which is the previous value associated with the key (in your case null
) as stated by Map#put 's javadoc
You simply need to do it in two steps :
Map<Object, Object> map = new HashMap<>();
map.put("key", "value")
list.add(map);
Or in one step (Fun but not recommended as this creates pointlessly a new anonymous class) :
list.add(new HashMap<Object, Object>() {
private static final long serialVersionUID = 1L;
{
put("key", "value");
}
});
Because the put method
Returns:
the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key.)
It returns the previous value and because your values are of type Object it returns an Object.
You cannot chain the Map creation and putting something into it together and have the Map returned. You need to split it into multiple steps:
Map<Object, Object> myMap = new HashMap<>();
myMap.put("key", "value");
list.add(myMap);
Map#put
returns the value associated with the key before put
was called (or null
if there was no such value), not the Map
itself.
If you want a oneliner to create a Map
, you can consider Collections#singletonMap
:
list.add(new HashMap<>(Collections.singletonMap("key", "value")));
(or, just drop the call to new HashMap
if you're OK with immutable maps)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.