File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "copy a list in a set" Watch "copy a list in a set" New topic

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?
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?


bye, <br />Marco
marc weber

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


Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: copy a list in a set
It's not a secret anymore!