Results 1 to 2 of 2
- 03-01-2012, 06:25 PM #1
Member
- Join Date
- Jan 2012
- Location
- Aberdeen
- Posts
- 5
- Rep Power
- 0
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:
Here is a test 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()); } } } } }
And it's my output: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(); } }
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 >
- 03-01-2012, 07:51 PM #2
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
-
Cannot return values from hashmap
By uhertz in forum New To JavaReplies: 1Last Post: 06-17-2011, 04:16 PM -
how to get the values from hashmap
By baktha.thalapathy in forum New To JavaReplies: 5Last Post: 05-25-2010, 02:12 PM -
Problems with hashMap, has values in it but can't find them?
By mainy in forum New To JavaReplies: 5Last Post: 07-28-2009, 10:22 PM -
HashMap contains all values but doesn't show all values
By xcallmejudasx in forum New To JavaReplies: 3Last Post: 05-10-2009, 11:35 PM -
how to return values from hashmap
By oregon in forum New To JavaReplies: 2Last Post: 08-01-2007, 04:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks