This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes Vector Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Vector Question" Watch "Vector Question" New topic
Author

Vector Question

Aravind Ramanujam
Greenhorn

Joined: Dec 14, 2004
Posts: 16
Hello

I have 2 vectors, vector1 has objects of person and vector2 has objects of company.

A new Vector should be created merging these 2 vectors. The object in the new vector has all the attributes from both Person and Company.


The constraint is, if the Id matches all the fields in Member object will be populated. If the id doesn't match, only the corresponding fields of the Member object will be populated.

Is there a easy way to compare each element of the 2 vectors? I tried a simple for loop iterating through the elements but its not working out right.

Any help is appreciated.
Thanks
A.R
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
Originally posted by Aravind:

The constraint is, if the Id matches


Whose id are you matching with who's ?

Assuming Member object's id with the id of Person and Company objetct's, then what does the following statement mean?

If the id doesn't match, only the corresponding fields of the Member object will be populated.


Can you please elaborate?


thanks
Harvinder
Aravind Ramanujam
Greenhorn

Joined: Dec 14, 2004
Posts: 16
Sorry i wasn't clear the first time. Hope the below example will make my question clear:

person1(Id:1, Name:John, Addr:3 Main st, City:Chicago, State:IL)
person2(Id:2, Name:Mike, Addr:12 6th st, City:San Francisco, State:CA)
person3(Id:3, Name: Diana, Addr:1635 Auburn Bl, City: Portland, State: OR)

comp1(Id:1, Company:XYZ, Position: Programmer)
comp3(Id:3, Company:HTC, Position:Sales Manager)
comp10(Id:10, Company:ABC, Position:Manager)

member1(Id:1, Name:John, Addr:3 Main st, City:Chicago, State:IL, Company:XYZ, Position: Programmer)
member2(Id:2, Name:Mike, Addr:12 6th st, City:San Francisco, State:CA, Company:, Position: )
member3(Id:3, Name: Diana, Addr:1635 Auburn Bl, City: Portland, State: OR, Company:HTC, Position:Sales Manager)
member10(Id:10, Name:, Addr:, City:, State:, Company:ABC, Position:Manager)

Information for Ids 1 & 3 are merged together. ID 2 has only person info and ID 10 has only company info.
[ December 09, 2008: Message edited by: Aravind Ramanujam ]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Are the two vectors guaranteed to be sorted? (in order of ID, ascending)
Aravind Ramanujam
Greenhorn

Joined: Dec 14, 2004
Posts: 16
No. They are not.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

hmph. for some reason, I'm not getting the code formatting. There's lots of extraneous code in here, but it's something you can copy/paste into an IDE and start tinkering with.

I was thinking if they were sorted, there would be a single-pass way to do it. But since they're not, this will do.


The output of the above, with my implementation, is:


The final output is not in the form of a Vector, but you could either post-process it into one, or use a Vector to start with, and instead of a key-value "mapping" in the HashMap, you can use the index of the merge vector itself (when adding to it, you'd be using the add method that inserts into a specified index). This implies then, you'd do *pre* processing, to determine how large your merge vector should be made.
[ December 09, 2008: Message edited by: Mike Curwen ]
 
GeeCON Prague 2014
 
subject: Vector Question