• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 883
  • Last Modified:

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() );
}
0
allelopath
Asked:
allelopath
1 Solution
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now