Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File comparison : How to convert String array to Map?  RSS feed

 
karthik chellappan
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've two text files:

File1: (id,name,role)

10,karthik,Developer
20,Arya,Tester

File2: (id,salary)

20,15000
10,10000

Expected Output:

10 Karthik Developer 10000
20 Arya Tester 15000

I've the following code so far:


I'm now stuck on how to use String arrays along with collections to get the desired result. i.e. if i have to use Map, then i'm not sure how to use key,value pair for the above scenario so that i can join 'salary' in file2 to other file1 data's based on 'id' value. Please guide me.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean how do you create an Employee object with a setSalary method?
 
karthik chellappan
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I want to know if i should be inserting all 'values' in Map in a for loop with 'id' as 'key'.If some one can give a code snippet of the remaining logic i can try it out.

Thanks.
 
karthik chellappan
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the code (like shown below) from above and now i'm getting the desired output. But i see that too much string object is created in loop which is not good. Can any one please tell me how i can optimize it and make it efficient.

 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using a tokenizer? Have you read what it says in its documentation?
You should add a few blank lines to that code, because it is not clear where the three loops begin and end.
I cannot see that you are creating too many String objects; I think that is unavoidable for that sort of exercise.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!