File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Convert from HashMap to ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert from HashMap to ArrayList" Watch "Convert from HashMap to ArrayList" New topic

Convert from HashMap to ArrayList

Kasey Austin

Joined: May 04, 2009
Posts: 2

I want to convert from a HashMap, Key Value pair 1 to 1, to a HashMap where the Keys are Strings and the Values are arraylist. This way, I can have 1 key mapped to many values.

This is the code:

When I try this I am getting this error:
The method put(String, ArrayList<String>) in the type Map<String,ArrayList<String>> is not applicable for the arguments (String, void)

Can you provide an example?
Thanks for your help.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

You're trying to put the return value of "add()" into the Map, which doesn't work since add() returns void. Instead, you just have to do this as two lines:

[Jess in Action][AskingGoodQuestions]
John de Michele

Joined: Mar 09, 2009
Posts: 600

Another way to do it would be like this:

Kasey Austin

Joined: May 04, 2009
Posts: 2
Thanks Ernest, Thanks John
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Kasey Austin wrote:Thanks Ernest, Thanks John

and welcome to javaranch
I agree. Here's the link:
subject: Convert from HashMap to ArrayList
It's not a secret anymore!