File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Assign value from one set to another set Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Assign value from one set to another set" Watch "Assign value from one set to another set" New topic
Author

Assign value from one set to another set

S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 241
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
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
Sheriff

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

Joined: Jun 03, 2009
Posts: 241
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
Sheriff

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

Joined: Jun 03, 2009
Posts: 241
Ritchie ,
Could you give one example , am not getting you ...

Satya
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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

Joined: Jun 03, 2009
Posts: 241
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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Assign value from one set to another set
 
Similar Threads
HashSet and equals()
Finish my code please
equals() vs inheritance
How to work with sets?
Comparing two collections...