How to convert hash map keys into list?


I have a hash map and I'm trying to convert the keys to a list. Here's the code:

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);
    }
}

When I try to do

List<ARecord> finalRecs = new ArrayList<ARecord>(uniqueRecs.keySet());

The error:

The constructor ArrayList(Set) is undefined".

How can I convert Hashmap keys to List<ARecord> finalRecs?


Answers:


Your uniqueRecs has String type of the key. You have to do:

List<String> keys = new ArrayList<>(uniqueRecs.keySet());

or

List<ARecord> values = new ArrayList<>(uniqueRecs.values());