我有一個靜態HashMap <Integer, String>和一個Arraylist<Integer>.
public static final HashMap<Integer, String> CONSTANT_MAP =
(HashMap<Integer, String>) Collections.unmodifiableMap(new HashMap<Integer, String>() {
{
put(28, "Action");
put(12, "Adventure");
put(16, "Animation");
put(35, "Comedy");
put(80, "Crime");
put(99, "Documentary");
put(18, "Drama");
put(10751, "Family");
put(14, "Fantasy");
put(36, "History");
put(27, "Horror");
put(10402, "Music");
put(9648, "Mystery");
put(10749, "Romance");
put(878, "Science Fiction");
put(10770, "TV Movie");
put(53, "Thriller");
put(10752, "War");
put(37, "Western");
}
});
我需要一種將HashMap 鍵與ArrayList 元素進行比較的方法。如果鍵相等,Hashmap則應將值分開ArrayList并回傳。
我嘗試了以下方法,但它不起作用。我明白了ExceptionInInitializerError。
public static ArrayList<String> getGender(ArrayList<Integer> arrayList, HashMap<Integer, String> hashMap) {
ArrayList<String> resultList = new ArrayList<String>();
for (Map.Entry m : hashMap.entrySet()) {
if (arrayList.contains(m.getValue()))
resultList = (ArrayList<String>) m.getValue();
}
return resultList;
}
uj5u.com熱心網友回復:
我明白了
ExceptionInInitializerError
此錯誤由欄位初始化期間或執行靜態初始化程式塊期間發生的例外觸發。來自Javadoc的參考:static
拋出An
ExceptionInInitializerError以指示在評估靜態初始化程式或靜態變數的初始化程式期間發生例外。
在這種情況下,您嘗試對 to 回傳的地圖進行型別轉換unmodifiableMap(),HashMap但此轉換失敗。它不能成功,因為unmodifiableMap()它旨在包裝Map介面的任何實作(TreeMap,LinkedHashMap等)。UnmodifiableMap這個方法回傳一個實作介面的類的實體Map(即它覆寫所有必需的方法)并維護一個型別的變數Map(作為引數傳遞的包裝映射)并委托給它的呼叫。本質上UnmodifiableMap和HashMap是兄弟姐妹,它們都實作了Map,但它們不相互擴展,因此您不能在這些型別之間進行轉換。
補救措施:針對介面而不是具體型別撰寫代碼,您會沒事的。應該是 type ,而不是(順便說一下,我建議使用更有意義的名稱,例如)。同樣,您的方法的引數應該是 a ,而不是。CONSTANT_MAPMapHashMapganreByIdListArrayList
我需要一種將
HashMap鍵與ArrayList元素進行比較的方法。如果鍵相等,Hashmap則應將值分開ArrayList并回傳。
首先,從地圖的內容來看,我相信你拼錯了方法名稱,你的意思是genre,不是gender。在下面的代碼中,我已經更改了它以及引數名稱(當然,名稱完全取決于您,這只是一個示例,但請記住,它們對代碼的可讀性有巨大影響,因此很重要) 和引數型別更改為介面型別List和Map.
處理此任務的最有效方法是遍歷串列并檢查特定元素是否包含在map中。
與您在代碼中所做的相反,因為contains()檢查串列的成本很高(您需要遍歷所有元素以找出匹配元素是否存在),如果您熟悉Big O符號。相反,檢查 a幾乎是即時的 - O(1)。containsKey()HashMap
如果匹配鍵存在,與之關聯的值將被添加到結果串列中。
public static List<String> getGenre(List<Integer> ids,
Map<Integer, String> genreById) {
List<String> result = new ArrayList<>();
for (Integer id: ids) {
if (genreById.containsKey(id))
result.add(genreById.get(id));
}
return result;
}
旁注:
- 如果此方法位于將映射定義為靜態欄位的同一類中,則將其作為引數傳遞是多余的。
- 您可以用 Java 9 可訪問的靜態方法替換雙花括號初始化(這不是一個好的做法
Map.ofEntries()) 。它回傳一個已經不可修改的映射,因此不需要用Collections.unmodifiableMap().
uj5u.com熱心網友回復:
好吧,如果您只想檢查 hashmap 中的鍵是否等于 arraylist,您可以使用hashMap.keySet().equals(new HashSet<Integer>(arrayList)), 然后使用hashMap.values()來獲取所有值。
uj5u.com熱心網友回復:
Alexander Ivanchenko 的回答正確地確定了您所看到的例外的原因(因此您應該接受他的回答!)。我的回答在下面討論了您的代碼的其他問題。
您的 hashmap 包含的值是字串:
HashMap<Integer, String> hashMap
所以
m.getValue()
是一個字串,因此您不能將其轉換為字串的 ArrayList
resultList = (ArrayList<String>) m.getValue();
我認為您想要的是在結果串列中累積值:
resultList.add(m.getValue());
此外,正如@racraman 所指出的,這種比較是有問題的:
if (arrayList.contains(m.getValue())
該串列arrayList包含整數;m.getValue() 是一個字串;我很驚訝這個編譯。我想從你的意思的描述
if (arrayList.contains(m.getKey())
所以寫這篇文章時可能是轉錄錯誤?
uj5u.com熱心網友回復:
長話短說,使用收藏庫為您完成作業:
public static List<String> getGenres(List<Integer> list, Map<Integer, String> map) {
return map.entrySet().stream()
.filter(e -> list.contains(e.getKey()))
.map(Map.Entry::getValue)
.collect(Collectors.toList());
}
還要注意抽象型別(例如List,Map等)優先于具體型別(例如ArrayList,HashMap等)的使用:參見 Liskov 替換原則 (LSP)
還要注意方法名稱 from getGenderto中拼寫錯誤的更正getGenres,更正語意和復數形式 - 回傳集合的方法應以復數形式命名。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/469975.html
