Monday, 19 August 2013

Sorting the values in the treemap

Sorting the values in the treemap

I read a text file and stored in a Tree map with each key having multiple
values.Like,
key: A1BG values: G5730 A4527 E3732 B0166
key: BCA3 values: C1478 A4172 D8974 B1432 E2147
key: DB8C values: N0124 K7414 X9851
Since it is tree map I got all the keys sorted.Now,I want to sort all
those values corresponding to the key.And get o/p As,
key: A1BG values: A4527 B0166 E3732 G5730
key: BCA3 values: A4172 B1432 C1478 D8974 E2147
key: DB8C values: K7414 N0124 X9851
Iam new to java.Can anyone help over this. Here is my code
BufferedReader reader = new BufferedReader(new
FileReader("E:\\book\\datasone.txt"));
Map<String, String> map = new TreeMap<String,String>();
String currentLine;
while ((currentLine = reader.readLine()) != null)
{
String[] pair = currentLine.split("\\s+");
key = pair[0];
value = pair[1];
if(map.containsKey(key))
{
value += map.get(key);
}
else
{
map.put(key,value);
}
}
for (String name: map.keySet())
{
String key =name.toString();
String value = map.get(name).toString();
System.out.println(key + " " + value+ " ");
}

No comments:

Post a Comment