我有一个散列映射,我正在尝试将键转换为列表。这是代码:
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());
错误:
The constructor ArrayList(Set) is undefined".
如何将 Hashmap 键转换为 List<ARecord>
最终记录?
最佳答案
您的 uniqueRecs
具有 String
类型的 key 。你必须做:
List<String> keys = new ArrayList<>(uniqueRecs.keySet());
或
List<ARecord> values = new ArrayList<>(uniqueRecs.values());
关于java - 如何将 HashMap 键转换为列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31820049/