![](/img/trans.png)
[英]Why does my program only show one of my Array <String> Food in all of my hashmap data instead of the actual values?
[英]Why does my Hashmap<Boolean,String> only have two entries?
我使用了HashMap來存儲布爾類型和indexName值的字典。 但是我注意到我的哈希圖最多只能增加2個。 為什么會發生這種情況,我該如何解決?
public Map<Boolean, String> findMetadata(String scanPackage) {
Map<Boolean, String> metadatas = new HashMap<>();
ClassPathScanningCandidateComponentProvider provider = createComponentScanner();
for (BeanDefinition beanDef : provider.findCandidateComponents(scanPackage)) {
try {
Class<?> cl = Class.forName(beanDef.getBeanClassName());
Indexable indexable = cl.getAnnotation(Indexable.class);
logger.info("---------------------------- " + indexable.dictionary() + " " + indexable.indexName());
if (!metadatas.containsValue(indexable.indexName())) {
metadatas.put(indexable.dictionary(), indexable.indexName());
}
} catch (ClassNotFoundException e) {
logger.error(ERROR + e);
}
}
return metadatas;
}
只需通過以下方式更改地圖聲明:
Map<String, Boolean> metadatas = new HashMap<>();
以及以這種方式放置的元數據:
metadatas.put(indexable.indexName(), indexable.dictionary());
也許這就是您真正想要做的。
顯而易見,您正在將布爾值用作哈希圖中的鍵。 現在,對於Hashmap,以下是正確的。 1.它應該具有唯一的鍵(否則,它將覆蓋存儲與鍵相對應的現有值)。 和2.布爾值可以具有兩個值true或false(不能為null),因此Map中只有兩個值。
如果您想擁有字典,則應按以下方式修改MAP。
Map<Integer, String> metadatas = new HashMap<Integer, String>();
索引:應該是一些數字,所以鍵是整數。 值應為字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.