0

im doing homework but im stuck in some doubts, my homework is about creating a mail account that sends and receives mail, but now I want to list only messages that were enchanged by users with a specified subject. im using lists and maps, and my idea is to create a tree Map that stores every message but I want to iterate them only by specified subject. example:

SUBJECT(command in main that searches for messages with a specific subject)

hello(subject user wants to find)

date | subject | email | text

2012-01-31 | hello | amigo@ptmail.com | everthings cool?

2012-01-29 | hello | outroamigo@ptmail.com | yes indeed.

//my construtor

public class MailClass implements Mail {

private List<Email> emails;

private List<Message> received;

private List<Message> sent;

private Map<String,Message> allMessages;

public CorreioClass()
{
    emails = new LinkedList<Email>();

    recebidas = new LinkedList<Mail>();

    enviadas=new LinkedList<Mail>();

    allMessages=new TreeMap<>();
}

public void send(String subject, String email, String text, String date) throws DuplicadoException
{
    if ( RepeteadedMessageSent(subject, email, date))
    {
        throw new DuplicadoException();

    } else
    {
        Message msgSend = new MessageClass(subject, email, text, date);

        sent.add(msgSend);

        allMessages.put(subject, msgSend);
    }
}

2 Answers2

1

If you're going to put the subject as key in a Map<String, Email> you'll need an exact match to find the accompanying email. As well as the issue of duplicate subjects overriding an Email object so you'll need to make it a Map<String, List<Email>> to prevent that which adds a bunch of complexity. If you do choose to go this route you can refer to this question to transform your list into a map: Java 8 List<V> into Map<K, V>

It's probably easier though to iterate the List of emails you have in memory and compare the subject to what you're looking for. This has the added benefit of being able to do partial matches or things like Levenshtein distance to also find approximate subject matches.

Plancke
  • 1,119
  • 11
  • 12
1

I think to satisfy the requirements of your assignment it would make the most sense to do the following:

  1. Leverage a Data Structure that allows you to reference objects for a given key ... that sounds like a Map!
  2. Use that Map to put key-value pairs when a new message is received
  3. If a subject key is occupied when another message is received, grab a reference to the list of existing emails and add the new message to the list
andrewdleach
  • 2,458
  • 2
  • 17
  • 25