I need to read two columns (both String) from a file and keep the values of the first column in a HashMap where the Integer is the counter.
For example if the file I am reading is
Apple Fruit
PC Device
Pen Tool
...
and the code is
String line="";
int counter=1;
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt"),"Unicode"));
while ((line = reader.readLine()) != null)
{
String[] words;
words= st.split(" ");
tokens.put(counter, words[0]);
counter+=1;
}
The problem is when I print The HashMap values, I found the values are in different order of that in the origianl file
for (Map.Entry<Integer, String> token:tokens.entrySet())
{
System.out.println(token.getKey() + token.getValue());
}
I got the following
1 Apple
3 Pen
4 whatever
2 ..etc
I do not know what is the problem?! can you please help me with that