簡體   English   中英

奇怪的錯誤Java Hashmap類型Long,String

[英]Weird error Java Hashmap types Long, String

我在Java程序中有一個很奇怪的行為。 我有一個從方法中檢索到的Long,Long哈希表,但是當我嘗試從該方法中獲取信息時,此哈希表是一個String,沒有任何錯誤或警告的String。在我的代碼下面:

List<HashMap<Long, Long>> getAggregateResult(String idCheptel, Integer limit) {
    StringBuilder hql = new StringBuilder();
    hql.append("select new map(t.prod.id, count(t.prod.id))");
    ... //rest of implem.
    return entityManager.createQuery(hql.toString()).setMaxResults(limit).getResultList();
}

函數調用

... //in other method try to retrieve the information
    for (HashMap<Long, Long> map : getAggregateResultVetProduct(idCheptel, limit)) {
            System.out.println(">>>>>>>" + map.get(0L)); //always null
            //the map is asked in Long, but in debugger it's a String, String
            for (Map.Entry<Long, Long> entry : map.entrySet()) {
                System.out.println("Key: " + entry.getKey()); //here key are 0
                System.out.println("Value: " + entry.getValue());
            }
        }

如果有人可以給我一些信息,為什么會有這種行為。 謝謝。

編輯

這里有一些信息:我正在使用Intellij。 在被問到調試器的屏幕截圖下方。

在此處輸入圖片說明

有關泛型的信息會在編譯過程中刪除。 因此,在運行時,您的方法僅返回HashMap 因此,有趣的問題是.getResultList()方法的返回值的實際類型是什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM