• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to copy the objects from set to the list

 
shyam ji gautam
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear all

let us suppose we have the objects of class person { string name ) and these objects is store into the set then how i will copy that objects from set to the list


set<person>

i want to copy that objects let us suppose that object1 is ("shyam" ) is store in set i want to copy it from set to the list <person>= new arraylist<person> like

please suggest me
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to have a look at the List.addAll() method.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Lists support duplicate values and ordering, which Sets don't. So you are losing information by putting things into a Set, which you cannot retrieve. If there is any way you can put them into a List at the outset, you can retain that information.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question: Do you want to copy the references to the Person or do you want to make a copy (clone) of the Person. It is a huge difference.

If I have this:


there is now 3 objects created. Two Person-objects and one ArrayList that holds references to the Persons.

If I now do this:


all I have done is creating a new TreeSet-object and copied the references from the pList into it.

However, if I want to be able to manipulate on a new object that is a copy of the original persons, then I need to do this:



We now have six object and even if I manipulate on the p1 or p2, it won't effect the copies of the Persons in the Set.

See http://en.wikipedia.org/wiki/Clone_%28Java_method%29
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic