Results 1 to 7 of 7
  1. #1
    mcnikal is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Default Printing a key and a value from a hashmap

    Hello,

    I have a hashmap of the form HashMap <String, Set<String>>

    I am trying to create a method with one argument. The argument is a key for the hashmap, if it exists it should print out the key and the associated values. I'm falling over at even getting it to print the key, it keeps printing all the keys from within the hashmap as I don't know how to load the argument into it.

    I have this so far

    Java Code:
     public void printValue(String club)
       {
              
          
         boolean result = clubMap.containsKey(club);
         if (result)
         {
            String key = clubMap.keySet(club).toString();
            System.out.println(key );
          }
          
          else
          {
             System.out.println("The key does not exist");
          }
         
       }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default Re: Printing a key and a value from a hashmap

    Does that code compile without errors? What version of java are you using?
    What is the definition of clubMap?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    mcnikal is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Default Re: Printing a key and a value from a hashmap

    Hello,

    No that code gives me an error. It gives me an error because of how I've incorrectly tried to load the argument into the clubMap.keySet if you remove the argument club from this then it compiles. So just have it as

    Java Code:
    public void printValue(String club)
      {
              
          
        boolean result = clubMap.containsKey(club);
        if (result)
        {
           String key = clubMap.keySet.toString();
           System.out.println(key );
         }
          
         else
         {
            System.out.println("The key does not exist");
         }
    clubMap is HashMap <String, Set<String>>

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default Re: Printing a key and a value from a hashmap

    No that code gives me an error.
    It be better if you'd post the error messages that the compiler found in the code rather than imply that the posted code compiled without errors.

    The posted code(in 3#) prints out the set of keys for the HashMap as is documented in the API doc.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    mcnikal is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Default Re: Printing a key and a value from a hashmap

    The method I'm trying to create should take an argument that specifies a key in the HashMap and then only print the values associated with that. The code I've managed to pull together prints everything in the hashmap, not just the specific key-value.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default Re: Printing a key and a value from a hashmap

    Look at the API doc for the HashMap class. It has a method that will return the value associated with a key.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default Re: Printing a key and a value from a hashmap

    If a String 'key' is present in that map and you want to print that key, simply print that 'key' variable. If you want to print the associated value set, get it from the map (given the 'key') and print them. You're over complicating matters ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 03-01-2012, 07:51 PM
  2. Replies: 0
    Last Post: 12-26-2011, 03:52 PM
  3. Replies: 9
    Last Post: 08-19-2011, 12:59 AM
  4. final HashMap hm=new HashMap();
    By sangramkeshari.jena in forum New To Java
    Replies: 4
    Last Post: 07-21-2011, 09:44 PM
  5. Replies: 7
    Last Post: 12-08-2009, 07:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •