aspose file tools*
The moose likes Beginning Java and the fly likes copy a list in a set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "copy a list in a set" Watch "copy a list in a set" New topic
Author

copy a list in a set

Marco Vanoli
Ranch Hand

Joined: Jan 12, 2005
Posts: 99
it is possible to copy one list in a set?
as:
List l = (List) x.getset();

My compiler give error.. is there a quickest way to not do a loop with iterator for addin every one set elements to the list?

tnKs


bye, <br />Marco
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You can't just cast a Set to type List because neither is a subtype of the other. However, you should be able to pass the Set instance to the constructor of a List (where it's upcast to Collection)...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You can also create a Set from a List with the correct constructor:

Set mySet = new HashSet(myList);

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: copy a list in a set