简体   繁体   English

如何在 HashMap 中存储和访问 2 种不同类型的列表

[英]How to store and access 2 different types of List in a HashMap

Im trying to store and access 2 different types of List in a HashMap, and access them when the method that return the HashMap is it called, but Im getting this error: The constructor ArrayList<JournalArticle>(Object) is undefined我试图在 HashMap 中存储和访问 2 种不同类型的列表,并在调用返回 HashMap 的方法时访问它们,但我收到此错误: The constructor ArrayList<JournalArticle>(Object) is undefined
The method looks like this:该方法如下所示:

public static HashMap<String, Object> getJournalArticles(long groupId) throws NumberFormatException, SystemException{
    List<JournalArticle> journalArticles = JournalArticleLocalServiceUtil.getStructureArticles(groupId);
    List<String> allJournalArticleIds = new ArrayList<String>();
    for (JournalArticle journalArticle : journalArticles) {
        allJournalArticleIds.add(journalArticle.getArticleId());
    }

    HashMap<String, Object> mapArticles = new HashMap<String,Object>();
    mapArticles.put("journalArticles", journalArticles);
    mapArticles.put("allJournalArticleIds", allJournalArticleIds);

    return mapArticles;
}

And when I call the method and try to store their respective values into a new List I get the error commented before:当我调用该方法并尝试将它们各自的值存储到一个新列表中时,我得到了之前注释的错误:

HashMap<String, Object> mapArticles = JournalArticleUtil.getJournalArticles(scopeGroupId);
List<JournalArticle> allArticles = new ArrayList<JournalArticle>(mapArticles.get("journalArticles"));
List<String> allJournalArticleIds = new ArrayList<String>(mapArticles.get("allJournalArticleIds"));

What´s wrong and how to solve?出了什么问题以及如何解决?

I would use a class written to hold this information (you may find it quicker to use something like Pair<L, R> ):我会使用编写的 class 来保存此信息(您可能会发现使用Pair<L, R>之类的东西更快):

class ArticleHolder {
    private List<JournalArticle> journalArticles;
    private List<String> allJournalArticleIds;

    public ArticleHolder(List<JournalArticle> journalArticles,
        List<String> allJournalArticleIds) {
        this.journalArticles = journalArticles;
        this.allJournalArticleIds = allJournalArticleIds;
    }

    //getters + setters
}

And change your methods:并改变你的方法:

public static ArticleHolder getJournalArticles(long groupId) 
    throws NumberFormatException, SystemException {

    List<JournalArticle> journalArticles = 
                JournalArticleLocalServiceUtil.getStructureArticles(groupId);
    List<String> allJournalArticleIds = new ArrayList<String>();

    for (JournalArticle journalArticle : journalArticles) {
        allJournalArticleIds.add(journalArticle.getArticleId());
    }

    return new ArticleHolder(journalArticles, allJournalArticleIds);
}

Beside that : your call to new ArrayList<JournalArticle>(...) shows that you intended to create new array list instances (assuming code could compile).除此之外:您对new ArrayList<JournalArticle>(...)的调用表明您打算创建新的数组列表实例(假设代码可以编译)。 There should be no need to do that, even if your map values were typed as Collection objects.即使您的 map 值被键入为Collection对象,也不应该这样做。

IMHO The quick solution is change the type of mapArticles to this HashMap<String, List<?>> and then:恕我直言,快速解决方案是将 mapArticles 的类型更改为此HashMap<String, List<?>>然后:

List<JournalArticle> allArticles = new ArrayList<JournalArticle>((Collection<JournalArticle>)mapArticles.get("journalArticles"));
List<String> allJournalArticleIds = new ArrayList<String>((Collection<String>)mapArticles.get("allJournalArticleIds"));

Because the ArrayList constructor only supports these options:因为 ArrayList 构造函数只支持这些选项:

 new ArrayList<T>();
 new ArrayList<T>(int capacity);
 new ArrayList<T>(Collection<? extends T> collection);

And Object isn't a collection at compiling time.并且Object在编译时不是一个集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM