如何打印HashMap <string,arraylist <mailitem =“” >>?

时间:2018-11-06 18:13:02

标签: java arraylist hashmap bluej

private HashMap<String, ArrayList<MailItem>> items;
/**
 * Construct a mail server.
 */
public MailServer()
{
    items = new HashMap<String, ArrayList<MailItem>>();
}

您如何在类中添加一种方法来遍历用户,打印出每个用户名,并打印出与该用户关联的所有电子邮件?基本上,您如何编写代码来遍历用户的哈希图,然后遍历用户的数组列表,并打印出每个用户的所有值?

1 个答案:

答案 0 :(得分:0)

public static void print(Map<String, List<MailItem>> map) {
    map.forEach((userName, mailItems) -> {
        System.out.println("User name: " + userName);
        System.out.println("User emails: " + mailItems.stream().map(MailItem::getEmail).collect(Collectors.joining(",")));
        System.out.println();
    });
}