| Author |
comparing and appending two vectors
|
Tariq Ahsan
Ranch Hand
Joined: Nov 03, 2003
Posts: 116
|
|
Hello, I am trying to compare the text strings separated by a delimiter (e.g. ,)of two separate files. The comparison is based on the first value of each of these files. I am thinking of using two separate vectors (also considering arraylist for this matter) to hold each line from these 2 files. Let me just illustrate what I like to achieve - vector #1 Vector1[0] : 0, 1, abc, cde Vector1[1] : 1, 1.1, efg, xxx Vector1[2] : 2, 2.6, xyz, abc vector #2 Vector2[0] : 0, 5, abd, cdef Vector2[1] : 2, 4.1, efg, ijk Vector2[2] : 1, 8.6, xyz, edf After the processing I would like to get - Vector1[0] : 0, 1, abc, cde, 5, abd, cdef Vector1[1] : 1, 1.1, efg, xxx, 8.6, xyz, edf Vector1[2] : 2, 2.6, xyz, abc, 4.1, efg, ijk Would appreciate if anyone could throw me some idea how I can get the above done.
|
 |
jiju ka
Ranch Hand
Joined: Oct 12, 2004
Posts: 302
|
|
You didn't ask for code. But I think this will get you going. All are static methods and very crude. You may refine it.
|
 |
jiju ka
Ranch Hand
Joined: Oct 12, 2004
Posts: 302
|
|
You may consider using an implementation of Set interface instead of Vector. By using set, all elements are guaranteed unique. You will not have to do the "!contains" filter. Vector is performance wise costly because it is synchronized. ArrayList is better than Vector. See api
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
Jiju's post was onto something important. For each key - the initial 0, 1, 2 in your input - gather up the lines that match. He changed your input format a bit, which you may not be able to do. See if this simplified version makes sense: Is parsing a string new to you? See indexOf and substring(). String.split() or StringTokenizer might look tempting, but they won't help you split on the first comma and preserve the other commas. Is Map new to you? See Map, HashMap and TreeMap. If you have the chance to change from Vector to one of the more modern types like List or Set, give them a close look, too. Let us know how it goes!
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Tariq Ahsan
Ranch Hand
Joined: Nov 03, 2003
Posts: 116
|
|
Hey Jiju & Stan, Thanks for the code and the tips! I'll try it out what you have suggested. Much appreciated for your help. Tariq
|
 |
Tariq Ahsan
Ranch Hand
Joined: Nov 03, 2003
Posts: 116
|
|
One other thing I like to mention which I've read in another post. Is somehow using HashMap instead will be a better preference? Thanks. Tariq
|
 |
Tariq Ahsan
Ranch Hand
Joined: Nov 03, 2003
Posts: 116
|
|
Sorry, Stan already mentioned about using HashMap. My mistake. Got trigger happy and shot out my earlier mail. Thanks. Tariq
|
 |
praveen Shangunathan
Ranch Hand
Joined: Sep 06, 2005
Posts: 64
|
|
You may consider using an implementation of Set interface instead of Vector. By using set, all elements are guaranteed unique. You will not have to do the "!contains" filter. Vector is performance wise costly because it is synchronized. ArrayList is better than Vector. See api ----------------------------------------- i thought this was till 1.2. now with 1.4 the performance lag is negligible. correct me if im wrong.
|
 |
jiju ka
Ranch Hand
Joined: Oct 12, 2004
Posts: 302
|
|
Praveen, Whether you should choose ArrayList or Vector depends on implementation. If threadsafety is not a concern use ArrayList kind else use Vector kind. http://www.javaworld.com/javaworld/javaqa/2001-06/03-qa-0622-vector.html http://www.javaranch.com/newsletter/June2002/listinterface.html [ November 23, 2005: Message edited by: jiju ka ]
|
 |
praveen Shangunathan
Ranch Hand
Joined: Sep 06, 2005
Posts: 64
|
|
|
thanks jijuka.
|
 |
 |
|
|
subject: comparing and appending two vectors
|
|
|