Set/Hashset keySet()

Posted on 2007-08-09
Last Modified: 2013-11-05
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) );
Question by:allelopath
    LVL 92

    Accepted Solution

    Set<String> referenceNumberSet = referenceMap.keySet();
    LVL 1

    Author Comment

    Huh, thought I tried though. guess not.
    LVL 86

    Expert Comment


    HashSet<String> referenceNumberSet = new HashSet<String>(referenceMap.keySet());

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Jasper Report: Crosstab Report- Include Page Footer 2 34
    dividesSelf challange 15 60
    commonTwo  challenge 63 69
    countHi2 challenge 7 30
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now