Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to print out all keys cerrently stored in a HashMap?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default How to print out all keys cerrently stored in a HashMap?

    I want to write a method to print the all the names of a phone book. phoneBook is a HashMap<String, String> , that has names as keys and phone numbers as values.

    Reading the documentation for both HashMap and Set, I have more or less an idea of how it could be done but I cant put it on code..
     
    public String getName()
        {
     
            String[] keys = phoneBook.keySet().toArray();
            return keys[0];
     
        }

    This is wrong. It doesnt compile saying incompatible types. I was thinking first to manage to succeed to get a key from my phone book and then I change it to print all the keys.
    Is this possible with this way of thinking?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to print out all keys cerrently stored in a HashMap?

    Use the API. What does Set.toArray() return?

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How to print out all keys cerrently stored in a HashMap?

    Nice one...I understood your point. I need the other toArray method that specify the return type Cheers!!!!!

    p.s. The Set.toArray() returns an Array of Set? (I ask to confirm that I completely understood)

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to print out all keys cerrently stored in a HashMap?

    Did you consult the Set API?

Similar Threads

  1. HashMap , trying to get the size of the value and NOT the keys.
    By Jad_Asmar in forum What's Wrong With My Code?
    Replies: 10
    Last Post: April 5th, 2014, 05:58 PM
  2. Please help with Arrow Keys
    By rtucker1023 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2013, 04:58 PM
  3. Replies: 1
    Last Post: December 3rd, 2012, 02:35 PM
  4. Overwrite what Keys do.
    By Melawe in forum Java Theory & Questions
    Replies: 4
    Last Post: May 17th, 2012, 11:23 PM
  5. combining keys
    By subhvi in forum Java SE APIs
    Replies: 10
    Last Post: August 29th, 2009, 04:35 PM