简体   繁体   中英

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
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 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). There should be no need to do that, even if your map values were typed as Collection objects.

IMHO The quick solution is change the type of mapArticles to this HashMap<String, List<?>> and then:

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:

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

And Object isn't a collection at compiling time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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