Mithrandir
Mithrandir

Reputation: 1

Removal of Class object key from Map in Java

I have a Map with a key/value pair made up of a string and class object Block. The remove(Object key, Object value) method fails to actually remove the object specified.

private static Map<String, ArrayList<Block>> stockInfo = new LinkedHashMap<>();

My IDE tips me off that my entry in the Object value data field "may not contain values of type 'Block' " What fundamental concept am I missing about Java and passing values in methods? I have only been coding in Java for a short period.

stockInfo.remove(symbolCode, (stockInfo.get(symbolCode)).get(0));

Upvotes: 0

Views: 223

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 338516

I have a Map with a key/value pair made up of a string and class object Block.

No, you don’t.

You declared a mapping of String to a collection of objects of type Block. Specifically an ArrayList collection.

So the compiler refuses your attempt to remove a string-to-block entry because there are no such entries. You have only string-to-list-of-blocks entries in your Map.

Map<String, ArrayList<Block>> stockInfo = new LinkedHashMap<>();

A mapping of string to block would look like this:

Map< String, Block > 

Upvotes: 1

Related Questions