wood burning stoves 2.0*
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
Author

how to copy the objects from set to the list

shyam ji gautam
Ranch Hand

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

Joined: Apr 06, 2010
Posts: 4343
    
    8

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

Joined: Oct 13, 2005
Posts: 37958
    
  22
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.

See http://en.wikipedia.org/wiki/Clone_%28Java_method%29
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to copy the objects from set to the list
 
Similar Threads
How to eliminate duplicates
hashing collision
ID:8 SCJP Question of the day !!!
Comparing objects
how to comapare the field value of an class objects which is save in set <hashmap>