我有一个哈希映射,我正在尝试将键转换为列表。这是代码:
List<ARecord> recs = new ArrayList<ARecord>();
HashMap<String, ARecord> uniqueRecs = new HashMap<String, ARecord>();
for(ARecord records:recs){
if(!uniqueRecs.containsKey(records.getId())){
uniqueRecs.put(records.getId(), records);
}
}
当我尝试去做
List<ARecord> finalRecs = new ArrayList<ARecord>(uniqueRecs.keySet());
错误:
如何将Hashmap键转换为
List<ARecord>
finalRecs? 最佳答案
您的uniqueRecs
具有 key 的String
类型。你必须做:
List<String> keys = new ArrayList<>(uniqueRecs.keySet());
要么
List<ARecord> values = new ArrayList<>(uniqueRecs.values());
关于java - 如何将哈希映射键转换为列表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31820049/