File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert from one collection to anonther..?" Watch "Convert from one collection to anonther..?" New topic
Author

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.


SCJP, SCWCD
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14108
    
  16


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
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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: 143

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

ohk...gotcha...Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert from one collection to anonther..?