Results 1 to 2 of 2
  1. #1
    adize is offline Member
    Join Date
    Jan 2012
    Location
    Aberdeen
    Posts
    5
    Rep Power
    0

    Default HashMap example - looking for and printing out the same values

    Hello,
    In my last lab assignment, I had to write a class which stores names and birthday dates in a HashMap. I managed to do this, as well as write methods to print only names, only dates and both key and value. However, I am struggling to complete the last part of an exercise.

    I was asked to write a method which iterates through the map and looks for people with the same birthday date. If it finds the same values, it will print out a list of the names together with the shared date of birth.

    I have written a method but I have a wrong output, as some values are printed twice. I would appreciate it so much, if you cold give me some advice how to tackle this problem.

    Here is my code for a class:

    Java Code:
    /**
     * @author AP
     * @version 1.0 01/03/2012
     */
    import java.util.*;
    
    
         public class Birthdays {
              
         //Fields
         private HashMap<String, String> birthdays;
         
    
         //Constructors
         public Birthdays()
         {
              birthdays = new HashMap<String, String>();
         }
         
         public void add(String name, String date)
         {
              birthdays.put(name,date);
         }
         
         public void printNames()
         {
              for(String name: birthdays.keySet())
              {
                   System.out.println(name);
              }
         }
         
         public void printNames2()
         {
              Iterator it = birthdays.keySet().iterator();
              
              while (it.hasNext())
              {
                   System.out.println(it.next());
              }
         
         }
         
         public void printDates()
         {
              for(String date : birthdays.values())
              {
                   System.out.println(date);
              }
         }
             
         public void printDates2()
         {
              Iterator it = birthdays.values().iterator();
              
              while (it.hasNext())
              {
                   System.out.println(it.next());
              }
         
         }
         
         public void printAll()
         {
              for (Map.Entry<String, String> entryPair:birthdays.entrySet())
              {
                   System.out.println(entryPair.getKey() + ": " + entryPair.getValue());
              }
         }
         
         public void printAll2()
         {
              Iterator it = birthdays.entrySet().iterator();
              
              while (it.hasNext())
              {
                   Map.Entry pair=(Map.Entry)it.next();
                   System.out.println(pair.getKey() + ": " + pair.getValue());
              }
         }
         
         public void hasSameBday()
         {
              for (Map.Entry<String, String> entry:birthdays.entrySet())
              {
                   for (Map.Entry<String, String> entry2:birthdays.entrySet())
                   {
                        if(entry.getValue().equals(entry2.getValue()) && !entry.equals(entry2))
                        {
                             System.out.println(entry.getKey() + ": " + entry.getValue());
                        }
                   }
              }
                   
    
         }
    }
    Here is a test class:
    Java Code:
    /**
     * A class that tests Birthdays class
     * @author AP
     * @version 1.0 01/03/2012
     */
    public class TestBirthdays {
         
          public static void main(String[] args) 
          {
               
               Birthdays b = new Birthdays();
               b.add("John", "1992/02/08");
               b.add("Sarah", "1987/03/07");
               b.add("David", "1987/03/07");
               b.add("Tom", "1992/07/06");
               b.add("Barbara", "1992/07/06");
               b.add("Samantha", "1992/07/06");
               
               b.hasSameBday();
       }
    }
    And it's my output:

    Java Code:
    > run TestBirthdays
    Samantha: 1992/07/06
    Samantha: 1992/07/06
    Barbara: 1992/07/06
    Barbara: 1992/07/06
    David: 1987/03/07
    Tom: 1992/07/06
    Tom: 1992/07/06
    Sarah: 1987/03/07
    >

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: HashMap example - looking for and printing out the same values

    I suggest you print out the info for both entry and entry2, that way you can see what is matching with what.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Cannot return values from hashmap
    By uhertz in forum New To Java
    Replies: 1
    Last Post: 06-17-2011, 04:16 PM
  2. how to get the values from hashmap
    By baktha.thalapathy in forum New To Java
    Replies: 5
    Last Post: 05-25-2010, 02:12 PM
  3. Replies: 5
    Last Post: 07-28-2009, 10:22 PM
  4. HashMap contains all values but doesn't show all values
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 05-10-2009, 11:35 PM
  5. how to return values from hashmap
    By oregon in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:56 PM

Tags for this Thread

Posting Permissions

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