This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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: 24189

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
Have you checked out Aspose?
subject: Convert from HashMap to ArrayList
It's not a secret anymore!