• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

copy a list in a set

 
Marco Vanoli
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)...
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also create a Set from a List with the correct constructor:

Set mySet = new HashSet(myList);

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic