• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to copy the objects from set to the list

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to have a look at the List.addAll() method.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I promise I will be the best, most loyal friend ever! All for this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic