Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3244432
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:33:02+00:00 2026-05-17T18:33:02+00:00

I have a practice project which I need help with. It’s a simple MailServer

  • 0

I have a practice project which I need help with. It’s a simple MailServer class. Here’s the code:

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.Collection;
import java.util.Map;

public class MailServer
{
    private HashMap<String, ArrayList<MailItem>> items;

    // mail item contains 4 strings:
    // MailItem(String from, String to, String subject, String message)

    public MailServer()
    {
        items = new HashMap<String, ArrayList<MailItem>>();
    }

    /**
     *
     */
    public void printMessagesSortedByRecipient()
    {
       TreeMap sortedItems = new TreeMap(items);

       Collection c = sortedItems.values();

       Iterator it = c.iterator();

       while(it.hasNext()) {
            // do something
       }
    }
}

I have a HashMap which contains a String key (mail recipient’s name) and the value contains an ArrayList of the mail for that particular recipient.

I need to sort the HashMap, and display the each user’s name, email subject, and message. I’m having trouble with this section.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:33:02+00:00Added an answer on May 17, 2026 at 6:33 pm

    You’re close.

       TreeMap sortedItems = new TreeMap(items);
    
       // keySet returns the Map's keys, which will be sorted because it's a treemap.
       for(Object s: sortedItems.keySet()) {
    
           // Yeah, I hate this too.
           String k = (String) s;
    
           // but now we have the key to the map.
    
           // Now you can get the MailItems.  This is the part you were missing.
           List<MailItem> listOfMailItems = items.get(s);
    
           // Iterate over this list for the associated MailItems
           for(MailItem mailItem: listOfMailItems) {
              System.out.println(mailItem.getSomething());
              }
           }
    

    You’ll have some cruft to clean up however – for instance, the TreeMap sortedItems = new TreeMap(items); can be improved.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In general, is it a best practice to have simple POJO Java classes implement
Is it good practice to have a class constructor that uses default parameters, or
Is there a best-practice or common way in JavaScript to have class members as
Currently I have an Area in my ASP.NET MVC 2 project call 'API', which
i have a input tag which is non editable, but some times i need
I have a two part question Best-Practice I have an algorithm that performs some
Is it good practice to have a unit test that specifies how long a
From what I have read best practice is to have classes based on an
Is it good practice to have more than one try{} catch{} statement per method?
Is it bad practice to have a string like name=Gina;postion= HouseMatriarch;id=1234 to hold state

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.