java - 将信息存储在数组列表中,该信息是 HashMap 中的值

标签 java arraylist hashmap store

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Iterator;
import java.util.Map;
import java.util.AbstractMap;

/**
 * A simple model of a mail server. The server is able to receive
 * mail items for storage, and deliver them to clients on demand.
 */
 public class MailServer
 {
    // Storage for the arbitrary number of mail items to be stored
    // on the server.
    private HashMap<String,ArrayList<MailItem>> mailMap;

    /**
     * Constructor
     */
    public MailServer()
    {
        mailMap = new HashMap<String,ArrayList<MailItem>>();
    }

    /**
     * Return how many mail items are waiting for a user.
     */
    public int howManyMailItems(String who)
    {
        int count = 0;
        for(ArrayList<MailItem> array : mailMap.values()) {
            for (MailItem item : array) {
                if (item.getTo().equals(who)) {
                    count++;
                }
            }
        }
        return count;
    }

    //  public int howManyMailItems(String who) 
    //  {    
    //      return mailMap.get(who).size(); 
    //  }

    /**
     * Return the next mail item for a user or null if there
     * are none.
     */
    public MailItem getNextMailItems(String who, int howMany)
    {
        // Access the ArrayList for "who" remove and return the first element
        // Be careful what if "who" doesn't have an entry in the mailMap
        // Or what if "who" doesn't have any mail?
        Iterator<Map.Entry<String, ArrayList<MailItem>>> it = 
        mailMap.entrySet().iterator();
        while(it.hasNext()) {
            Map.Entry<String, ArrayList<MailItem>> entry = it.next();
            String key = entry.getKey();
            ArrayList<MailItem> value = entry.getValue();
            if (key.equals(who));
            {
                return value.remove(0);
            }
        }
        return null;
    }

    /**
     * Add the given mail item to the message list.
     */
    public void post(String who)
    {
        if (mailMap.containsKey(who)) {
            Map.put(who, Map.get(who) + 1);
        }

    }
}

上面的代码适用于基本的邮件服务器。我试图让它在 HashMap 中存储 MailItem(字符串收件人、字符串主题、字符串消息),其中包含字符串键和 ArrayList(MailItems)值。

我遇到的问题是 post() 方法。我不知道如何让它获取消息的发送对象的参数并将其存储在相应的 ArrayList 中。

我在使用 getNextMailItems() 方法时也遇到问题。我不知道如何让它从收件人的 ArrayList 返回多个项目。我所能想到的就是添加一个参数来指定要返回的 MailItems 数量。

我对 java 非常缺乏经验,并且仍在学习中。请帮忙。谢谢大家。

最佳答案

既然你正在学习Java,让我指出几点:

  • 请尝试 Guava MultiMap 而不是更丑陋的:

    private HashMap<String,ArrayList<MailItem>> mailMap;
    

    还尝试使用接口(interface)而不是类,即 Map<String,List<MailItem>会更好,因为它不会将您与特定的实现联系起来。

  • 这不是最佳选择,我怀疑是错误的:

    int count = 0;
    for(ArrayList<MailItem> array : mailMap.values()) {
      for (MailItem item : array) {
        if (item.getTo().equals(who)) {
          count++;
        }
      }
    }
    

    你应该打电话get(who)在 map 上,如果您获得非空列表,您将获得 MailItem 的列表为who 。然后调用List.size()会给你你需要的计数(我怀疑)。

  • 关于此代码块的两个注释:

    Iterator<Map.Entry<String, ArrayList<MailItem>>> it = mailMap.entrySet().iterator();
    while(it.hasNext()) {
      Map.Entry<String, ArrayList<MailItem>> entry = it.next();
      String key = entry.getKey();
      ArrayList<MailItem> value = entry.getValue();
      if (key.equals(who)); // the semicolon here means that if the condition is true, do nothing
      {
        return value.remove(0); // this block is ALWAYS EXECUTED
      }
    }
    return null;
    

    如果 if 中的条件为 true,则不会因为分号而执行任何操作,并且代码块中的代码将始终第一次执行。也就是说,您再次获得了 key ,因此您无需遍历整个 map (请参阅上一点)。但是,如果您想迭代 map ,请使用更紧凑的习惯用法:

    for(Map.Entry<String,ArrayList<MailItem>> e : mailMap.entrySet()){
      // do something with the key and value, e.getKey(), e.getValue()
    }
    
  • 最后,这个 block 令人费解:

    if (mailMap.containsKey(who)) {
      Map.put(who, Map.get(who) + 1);
    }
    

    你想做什么? Map是一个接口(interface)的名称,没有静态方法putget所以这甚至无法编译。如果您尝试将元素添加到 mailMap对于 key who那么你可能想要类似的东西(列表映射的典型习语,再次参见Guava以获得更好的API):

    ArrayList<MailItem> l = mailMap.get(who);
    if(l == null){ // this super verbose code will simplify with the Guava MultiMap
      l = new ArrayList<MailItem>();
      mailMap.put(who, l);
    }
    l.add(/* code to create a new MailItem */);
    

关于java - 将信息存储在数组列表中,该信息是 HashMap 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22315886/

相关文章:

java - 如何创建生成随机数数组列表的方法

java - 并行迭代大 HashMap

android - 读取 .txt 并将信息放入 hashMap

java - Concurrent HashMap merge() 和 put() 的区别

android - 根据 ArrayList 中保存的值设置文本

JavaFX 显示 WebView 控件的 HTML 源代码

java - android定时器无论如何都可以工作

java - 时间复杂度理论与线性搜索的实际实验不匹配

java - JVM元空间在调整大小时是否总是触发GC

java - 如何从嵌套的 ArrayList 访问特定元素