【发布时间】:2012-11-22 13:23:45
【问题描述】:
文件是这样的:
name1 134.2
name2 456.7
name3 265.3
...
...
我读取文本文件并存储在 HashMap 之后我想按顺序排序(按最大值),但问题是因为我对字符串中的值进行排序,所以我无法比较它。 那么..有没有办法将 textfile 的值以 double 或 integer 形式放入 hashmap 中?
import java.io.*;
import java.util.*;
class Test
{
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new FileReader("score.txt"));
HashMap<String, String> map = new HashMap<String, String>();
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("\t\t");
map.put(columns[0], columns[1]);
}
System.out.println(map);
}
}
【问题讨论】:
标签: java io hashmap comparator