wood burning stoves
The moose likes Java in General and the fly likes List to Map conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "List to Map conversion" Watch "List to Map conversion" New topic

List to Map conversion

Nicolas Flammel
Ranch Hand

Joined: May 05, 2004
Posts: 32
How can I convert a list to a map object?
[ May 26, 2004: Message edited by: Nicolas Flammel ]
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

There is no addAll(Collection) method for Map, (there is one defined in the Collection interface, but it is unsupported by the Map class) so I think you will have to iterate through the List and add each entry to the Map yourself.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Yes, Paul is right. Note that there's certainly no obvious way that such an addAll() could be implemented: what would it use for the keys?

[Jess in Action][AskingGoodQuestions]
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Intermediate forum...

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
If my memory serves me well, I remember this topic has been discussed somewhere here or in Java Developers Forum...

Anyway, a map stores a key-value pair, but a list don't. So if you're to convert your list into a map, how would you want to access your contents? In short, what is the key that identifies your value in the map going to be?

SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
I agree. Here's the link: http://aspose.com/file-tools
subject: List to Map conversion
It's not a secret anymore!