2016-08-05 94 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>(); 然後,地圖存儲鍵值整數集。如果你希望你的地圖是對象並返回一個整數類型,你應該投的對象爲整數。