TransWikia.com

Can not put a T in Map

Stack Overflow Asked by Hemant on November 25, 2020

private static final Map<String, ? extends TrackingInterface<TrackResponseObject>> map = new HashMap<>();
    static {
        map.put("dunzo", new DunzoShipment());
        map.put("self", new SelfShipment());
    }
public class DunzoShipment implements TrackingInterface<TrackResponseObject>,java.util.function.Supplier<TrackingInterface<TrackResponseObject>> { 
//body
}

while puting the object into map i am getting an error:

The method put(String, capture#1-of ? extends TrackingInterface) in the type Map<String,capture#1-of ? extends TrackingInterface> is not applicable for the arguments (String, DunzoShipment)

One Answer

? extends TrackingInterface

means "something which extends TrackingInterface". We don't know what that something is, so we can't be sure that DunzoShipment is one of them. That's why you can't insert into the map.

PECS: producer extends, consumer super. This is a consumer (via put), so you cannot use extends.

Just remove the ? extends part and it will compile.

Correct answer by Michael on November 25, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP