• Post Reply Bookmark Topic Watch Topic
  • New Topic

help with HashMap  RSS feed

 
Sam Smythe
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an assignment I have to do and I do NOT expect anyone to do the coding for me. I am just looking for some direction as I have no clue where to even begin. Any guidance would be very welcomed.

We are given the code pasted below and one of our challenges is to "Modify the MailServer so that it uses a HashMap to store MailItems. The keys to the HashMap should be the names of the recipients, and each value should be an ArrayList containing all the MailItems stored for that recipient. "

import java.util.ArrayList;
import java.util.Iterator;

/**
* A simple model of a mail server. The server is able to receive
* messages for storage, and deliver them to clients on demand.
* @author David J. Barnes and Michael Kolling
* @version 2001.05.30
*/
public class MailServer
{
// Storage for the arbitrary number of messages to be stored
// on the server.
private ArrayList messages;

/**
* Construct a mail server.
*/
public MailServer()
{
messages = new ArrayList();
}

/**
* @return How many messages are waiting for the given user.
* @param who The user to check for.
*/
public int howManyMessages(String who)
{
int count = 0;
Iterator it = messages.iterator();
while(it.hasNext()) {
MailItem mess = (MailItem) it.next();
if(mess.getTo().equals(who)) {
count++;
}
}
return count;
}

/**
* Return the next message for who. Return null if there
* are none.
* @param who The user requesting their next message.
*/
public MailItem getNextMailItem(String who)
{
Iterator it = messages.iterator();
while(it.hasNext()) {
MailItem mess = (MailItem) it.next();
if(mess.getTo().equals(who)) {
it.remove();
return mess;
}
}
return null;
}

/**
* Add the given message to the message list.
* @param item The mail item to be stored on the server.
*/
public void post(MailItem item)
{
messages.add(item);
}
}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, obviously, every method that touches "messages" needs to change. SOme of them get simpler (howManyMessages, getNextMessage) and some get more complicated (add).

What add has to do is something like this:

- get the name from the Mailitem
- check in the HashMap for a value for the name as a key
- If there's no value, then
... create an arraylist
... add the Mailitem to it
... store the arraylist in the HashMap with the name as the key
- If there *is* a value then
... it's an ArrayList
... store the Mailitem in it.

Now, think about what "howManyItems" has to do, and you describe it to me!
 
Sam Smythe
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much. I think I have a good start to the HashMap talking to the list. I am now stuck on the getNextMailItem method and I think if I can get that, I can get the rest. I have a compiler error saying I have else without if, even though I definitely have the if statement, so I can't even test if what I am doing works. Can you tell me if I am at least on the right track?

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class MailServer
{
// Storage for the arbitrary number of messages to be stored
// on the server.
private HashMap messages;
private ArrayList list;
private MailItem item;

/**
* Construct a mail server.
*/
public MailServer()
{
messages = new HashMap();
list = new ArrayList();
}

/**
* Return the next message for who. Return null if there
* are none.
* @param who The user requesting their next message.
*/
public MailItem getNextMailItem(String who)
{
Iterator it = list.iterator();
while(it.hasNext())
{
MailItem item = (MailItem)it.next();

if(messages.containsKey(who));
{
return item;
}

else
{
return null;
}
}
}



/**
* Add the given message to the message list.
* @param item The mail item to be stored on the server.
*/
public void post(String name, MailItem item)
{
if(messages.containsKey(name))
{
list = (ArrayList) messages.get(name);
}
else {
list = new ArrayList();
list.add(item);
}
messages.put(name, list);
}
}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're getting the error message because of an extra semicolon:

if(messages.containsKey(who));

means



Removing that semicolon would fix the error because the following set of braces would become the body of the "if", as you intended.

But you're not on the right track, actually. getNextMailItem() should actually be vastly simpler than this. Hint: you don't need an iterator at all. Read the Javadoc for HashMap -- the part at the top that describes the class itself -- and you should get a clearer idea of what to do.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!