• 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

Assign value from one set to another set

 
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I have two HashSets which have 2 objects of type ClassRoom.
I want to take one HashSet's value and put into another HashSet.

Here is my approach :




Is this approach is good ?

It's appreciable if anyone give better approach

regards,
Satya
 
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are far simpler ways. You can pass the entire contents of a Set to another. I suggest you look at the methods of the Set interface, and the constructors of one of the Set implementations.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What on earth is that loop with the break; in for? Since the order of iteration of a Set is undefined, the results of that loop will be undefined and unpredictable.
You ought also to have all the fields in that Classroom class private and accessed by getXXX methods.
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ritchie ,, thanks for the reply ...Yes there are few methods by which we can put all values from one set to another .

But, question arise when I want to persist only one set of objects and do the rest of my work by that set .Then I have to put each object's data one by one to another set of object (here ClassRoom ...).

Correct me if am wrong .


Satya

 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are going to have to use some sort of filter if you wish to pass some values only. Do it with an if (...) {...otherSet.add(...);} statement inside the loop. The break; will not work as you expect.
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ritchie ,
Could you give one example , am not getting you ...

Satya
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your intent isn't quite clear to me. Are you saying you are copying the attributes from the elements of one set onto the members of the second set?
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dennis Deems ,
I have to persist only one set of objects , (here set1) , getting updated data from set2 and putting into set1.
I can't change the objects (here ClassRoom) into the set1.So copping all attributes from one set of objects (set2) to another set (set1).
satya
reply
    Bookmark Topic Watch Topic
  • New Topic