How to convert hash card keys to list?

I have a hash map and am trying to convert keys to a list. Here is 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()); 

Mistake:

The constructor of ArrayList (Set) is not defined. "

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

+12
java arraylist hashmap
source share
3 answers

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

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

or

 List<ARecord> values = new ArrayList<>(uniqueRecs.values()); 
+49
source share

What worked for me, that I wanted to modify the existing list:

 list.addAll(map.keySet()); 
+5
source share

In Java 1.7 and 1.8, the following works:

 List<ARecord> finalRecs = new ArrayList<ARecord>(); for (final String key : uniqueRecs.keySet()) { finalRec.add(new ARecord() { public String gettld() { return key; } }); } 
0
source share

All Articles