my dog learned polymorphism
The moose likes Beginning Java and the fly likes how to copy the objects from set to the list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to copy the objects from set to the list" Watch "how to copy the objects from set to the list" New topic

how to copy the objects from set to the list

shyam ji gautam
Ranch Hand

Joined: Sep 17, 2011
Posts: 49
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


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

Joined: Apr 06, 2010
Posts: 4543

You might want to have a look at the List.addAll() method.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46362
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

Joined: Mar 10, 2008
Posts: 326

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.

I agree. Here's the link:
subject: how to copy the objects from set to the list
It's not a secret anymore!