This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • 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