2016-08-05 67 views
-1

我想给getMap一个整数值超载,但地图只返回一个对象。无论如何要转换它?或者有类似于C++自动说明符的东西?如何对象转换为INT

public class EventMessage<T> { 

    private final Map<EventType, T> messageInput; 

    public EventMessage(EventType type, T t) { 
     messageInput = new HashMap<EventType, T>(); 
     messageInput.put(type, t); 
    } 

    public Map<EventType, T> getMap(){ 
     return messageInput; 
    } 

    public T getMap(EventType type){ 
     return messageInput.get(type); 
    } 
} 
+1

创建'事件消息'在你的代码,并使用整型。 –

+1

'getMap'返回'T',如果你想让它返回'Integer',你应该实例化一个'事件消息' –

回答

0

默认情况下,Map和所有的Collections对象都存储在它们中。如果你想指定你应该使用泛型的东西。 例如,对于与整数a地图,你应该写 Map<Integer, Integer> map = new HashMap<Integer,Integer>(); 然后,地图存储键值整数集。如果你希望你的地图是对象并返回一个整数类型,你应该投的对象为整数。