How to convert hash map keys into list?

95,475

Solution 1

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

Solution 2

What worked for me that i wanted to modify an existing list was:

list.addAll(map.keySet());

Solution 3

I think the best way to do this 'nowadays' is:

List<Integer> result = map.keySet().stream().collect(Collectors.toList());

This will not throw any warnings and will get the List<Integer> generic correctly.

Share:
95,475

Related videos on Youtube

Jay
Author by

Jay

Updated on December 14, 2020

Comments

  • Jay
    Jay over 3 years

    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?

  • Jay
    Jay almost 9 years
    I get an error"The method addAll(Collection<? extends ARecord>) in the type List<ARecord> is not applicable for the arguments (Set<String>)"
  • schtever
    schtever almost 9 years
    @Jay revised the answer to populate the list with the Map's values not the keys.
  • Jay
    Jay almost 9 years
    would there be any way to convert List<String> keys into List<ARecord>?
  • Jay
    Jay almost 9 years
    I actually want the keys. Is there any way to get keys and convert to List<ARecord>?
  • ka4eli
    ka4eli almost 9 years
    ARecords in you map are not the keys, they are values! You have to decide list of what entities you want to get in the end: ARecords or Strings. If ARecords - then take values of a Map, if Strings - take keySet.
  • Jay
    Jay almost 9 years
    I want keySet. Once I get that, I pass into another method which takes in List<ARecord>. I can't figure out how to pass the keys of hash map as a List<ARecord>
  • Louis Wasserman
    Louis Wasserman almost 9 years
    @Jay: That statement makes no sense. The keys are Strings. "how to pass the keys of hash map as a List<ARecord>" makes no sense, because the keys are not ARecords. That's the values.
  • Jay
    Jay almost 9 years
    it's an interface: public interface ARecord {public String gettId()}
  • Mick Mnemonic
    Mick Mnemonic almost 9 years
    @Jay, if the records in uniqueRecs are indeed unique as the name implies, meaning that all ARecords in the map have unique ids, then you can simply use new ArrayList<>(uniqueRecs.values()) as ka4eli is suggesting here.
  • Mark
    Mark over 7 years
    Thank you - to elaborate/explain further keySet() returns a Set and this extends the Collection class. values() returns a Collection. This : public ArrayList(Collection<? extends E> c) from ArrayList constructor makes it valid.