0

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?

2
  • 1
    because Map.put() returnes the value, not the map itself. Feb 2, 2018 at 13:00
  • Tried Map<Object, Object> on the last sample. It worked. Will try to investigate some cases for a while.
    – Rei Brown
    Feb 2, 2018 at 13:03

5 Answers 5

3

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);
4
  • Or list.add(Map.of("key", "value")) (assuming you don't care about subsequent mutability of the map). Feb 2, 2018 at 13:01
  • 'put()' returns the value previously associated with the specified key. It does not return the key Feb 2, 2018 at 13:02
  • What is this Map.of?
    – Rei Brown
    Feb 2, 2018 at 13:05
  • @RobertKock Fixed it in my post. Thank you
    – locus2k
    Feb 2, 2018 at 13:05
2

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);
2

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)

2

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"

2

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");
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.