【问题标题】:Storing info in an arraylist that is the value in a hashmap将信息存储在作为哈希图中值的数组列表中
【发布时间】:2014-04-14 11:37:06
【问题描述】:
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(String recipient, String subject, String message),其中包含一个 String 键和一个 ArrayList (of MailItems) 值。

我遇到的问题是 post() 方法。我不知道如何让它接受消息的目标参数并将其存储在相应的 ArrayList 中。

我的 getNextMailItems() 方法也有问题。我不知道如何让它从收件人的 ArrayList 中返回多个项目。我所能想出的只是添加一个参数,指定要返回多少 MailItems。

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

【问题讨论】:

  • 我建议查看 Guava Multimap,它用更直观、更强大的实现替换了列表映射。
  • 我正在做的项目需要一个带有 ArrayList 的 HashMap
  • 是的,但我不想作弊。我现在没有任何可用的帮助。有人建议我将堆栈溢出作为寻求帮助的好方法。

标签: java arraylist hashmap store


【解决方案1】:

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

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

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

    还可以尝试使用接口而不是类,即Map&lt;String,List&lt;MailItem&gt; 会更好,因为它不会将您绑定到特定的实现。

  • 这不是最优的,我怀疑是错误的:

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

    你应该在地图上调用get(who),如果你得到一个非空列表,你将有一个MailItems 的列表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 中的条件为真,则不会因为分号而执行任何操作,并且代码块中的代码将始终第一次时执行。也就是说,您再次获得了密钥,因此您无需遍历整个地图(请参阅上一点)。但是,如果您想迭代地图,请使用更紧凑的习惯用法:

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

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

    你想做什么? Map 是接口的名称,没有静态方法putget,因此甚至无法编译。如果您尝试为键 who 添加元素到 mailMap,那么您可能需要类似(列表映射的典型习语,再次参见 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 */);
    

【讨论】:

  • 非常感谢您的详尽解释。我想我理解并会尝试建议的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-18
  • 2016-06-16
相关资源
最近更新 更多