• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to add each row to a hashmap and then add it into a List?  RSS feed

 
reetha raj
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a text file of n rows and n columns separated by a tab space to be read into a hashmap...Here, the first column is to be skipped.. the second column should be the key and the remaining columns should be the value to the key. The 2 issues I face is..1)how to skip reading first column(Because,as of now, it takes first column as key and rest as values) and 2) How do I add each row into a map and then add everything it into a list? This is what i have as of now..
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I presume you want your Reader to stop when it encounters the first 0‑length line, even though there are more lines in the file.
Why are you using a FileInputStream rather than a FileReader for a text file?
I would suggest you are going about writing code the wrong way if you write a whole block of code like that. Break it into smaller tasks. I suspect you may be able to refactor that block into three methods, one to read the file, one to add to the Map, and one to add to the List. Remember that code which reads text files like that is brittle; it is very tightly coupled to the format of the text file.
I would suggest the first thing to do is to write code which reads each line and simply prints it to the command line. This will verify that you are reading the file correctly.
Then you can add the splitting code. Then write code to add each line to a List. Then add code to add each line to the Map. You do not have to do all these things in that particular order.

The reason you have put the contents of the first column into the Map as its “K” is that you told the code to use the first column. You used index [0].
I think you are being slack in what you wrote. You said you are delimiting the file with a tab space. I don't think there is any such thing. Do you mean a tab which is one character, or do you mean tab‑space which is two characters?
You are also splitting the String and adding it back together. Why? You know you can give the split method a parameter which is the maximum size of the array to split into. It will stop splitting when it reaches that number. Are you simply doing this to remove tab characters? Have you considered the replacing methods of the String class? Or a StringBuilder where you can iterate it and remove any tab characters?
 
reetha raj
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi, Campbell Ritchie:
Thank you for your suggestions. I am new to coding with java and so facing difficulties to render a good quality code..

Its a tab(one character) between the columns in my file.

Before processing with map,I have already tried reading the file and printing it to see if it renders properly as per my input text file.

For my inner map, i avoid the first column, store second column in the key and the remaining column as values.So,I split the file to store the key in a variable and the values in other variable to later add it into the map. But, Again i add it up because I need to store the values in the map... If i dont add up, and as i have more than one value for the same key, I do not know how to go with the map.put() with one key and more than one value(As I should use only map and not multimap as per the requirement).

Assuming my text file contains 5 rows, I should store each row in a each map and then add all these map into a list... and then Create anothe rmap which contains first column as key and the list(of map) as the value .... and am stuck up how to do that..
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome

Your reply sounds confused.
The way to put the second entry as the “K” is to tell the program to use the second entry, by using the appropriate index in the array. You would appear to know how to do that, but DO NOT go back to the original code and edit it. That makes me look stupid going on about index [0].
You say you are assuming there are 5 entries per row. How do you know the original file is correctly formatted? You can split the String and test for the length of the resultant array; that is easy enough. I shall let you decide what to do if the line is in the wrong format.
You have the String which was the line read, though you are giving it the poor name of lineReader. Maybe if you called it line, you would find it easier to visualise. It is very easy to add that line to a List<String>.
What do you mean by inner Map? Do you have two Maps? Are you returning that Map from a method? If you are returning the Map, what are you doing with the List?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier, I wrote: . . .
You say you are assuming there are 5 entries per row. . . .
Sorry, I misread what you posted. You said 5 rows.

It does not make any difference how many rows there are; the program will run exactly the same for 5 rows or 5000000 rows, but you might run out of memory at 5000000000
The only difference is, that you can give the Map and List a predefined capacity if you now you have a large file, and that might improve performance slightly. So you need not do anything about number of rows.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!