Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing and appending two vectors

 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, Stan already mentioned about using HashMap. My mistake. Got trigger happy and shot out my earlier mail.

Thanks.

Tariq
 
praveen Shangunathan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks jijuka.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic