Set/Hashset keySet()

How do I get the keys from a HashMap?
In the code snippet below, I get this compile error:
Cannot convert from Set<String> to HashSet<String>

Map<String, Integer> referenceMap;

HashSet<String> referenceNumberSet = new HashSet<String>();
referenceNumberSet = referenceMap.keySet();
            
Iterator<String> iterator = referenceNumberSet.iterator();
while ( iterator.hasNext()) {
  System.out.println( (String) iterator.next() );
}
LVL 1
allelopathAsked:
Who is Participating?
 
objectsCommented:
Set<String> referenceNumberSet = referenceMap.keySet();
0
 
allelopathAuthor Commented:
Huh, thought I tried though. guess not.
0
 
CEHJCommented:
Try

HashSet<String> referenceNumberSet = new HashSet<String>(referenceMap.keySet());
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.