wood burning stoves 2.0*
The moose likes Java in General and the fly likes Convert from one collection to anonther..? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert from one collection to anonther..?" Watch "Convert from one collection to anonther..?" New topic

Convert from one collection to anonther..?

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

Hi I want to know the way to convert one collection like ArrayList to some Map type or Set type...?

Or what about within same interface like from ArrayList to Vector.............?

No Kaustubh No Fun, Know Kaustubh Know Fun..
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

what about within same interface like from ArrayList to Vector.............?

You can pass arraylist object (a) to vector constructor as

or using Collections.copy(v,a); you can copy elements.

Life is easy because we write the source code.....
Garik Ustinov
Ranch Hand

Joined: Jun 22, 2009
Posts: 31
I think all implementations of Collection have a constructor, which takes another Collection and adds all the elements to the new object, like new ArrayList(someHashSet).
Speaking of Map type, we have a problem, since Map is not a Collection and it's entry needs two things - key and value, while Collections can provide only one element, so you have to populate a Map manually.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

A Map and a Set have different structures than a List, so there is no single, clear way to convert a List to a Map or Set.

You can create a Set and add all the elements of the List to it. Since a Set cannot contain duplicate elements, the content of the Set might be different than what was in the original List:

Note: Vector is a legacy collection class; you should not use it (unless there is a special reason to do so, for example because you're using an old API that expects a Vector).

It's not so simple to convert a List to a Map; you'll have to decide what the keys and values in the Map are going to be, based on the elements in the List.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor

Joined: Oct 27, 2005
Posts: 19655

Most Collection classes have a constructor that take any other Collection. Even if they don't, each Collection has the addAll method. If the collection allows adding elements then you can create an empty collection, then call this method:
Map is more difficult since you want to convert a "1 dimensional" structure to a "2 dimensional" structure. What will the values for the keys be?

The other way around (Map to Collection) will be easier since Map has three Collection views (keySet(), values(), entrySet()) which already turn the Map into a Collection (well, kind of).

How To Ask Questions How To Answer Questions
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

to classes belongs to same interface it is easy to convert....but for classes with differenent interface(taking about Map) is it difficult or not possible?
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 142

No, you can not convert a Collection to a Map, because it has an entirely different structure. You would have to do this manually, and even that takes some effort as you have to specify keys and values, not just values like in a Collection.

Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

Don't get me started about those stupid light bulbs.
subject: Convert from one collection to anonther..?
Similar Threads
Convert a String[] array
How to get each value from an ArrayList of type STRING in JSF
error please help
Collections and Generics Help
2-d arrays