我需要将 Dates.txt 文件中的以下日期放入 HashMap 中。
1 05.05.2017
2 11.12.2018
3 05.30.2020
4 03.15.2011
5 12.15.2000
6 10.30.2010
7 01.01.2004
8 12.31.1900
9 02.28.2014
10 10.10.2012
HashMap 的格式应该是
初始哈希图:
2017-05-05:1
2018-11-12:2
2020-05-30:3
2011-03-15:4
2000-12-15:5
2010-10-30:6
2004-01-01:7
1900-12-31:8
2014-02-28:9
2012-10-10:10
到目前为止我的代码片段
public class driver {
public static void main(String[] args) throws FileNotFoundException {
HashMap <LocalDate, Integer> dates = new HashMap <LocalDate, Integer>();
Scanner s = new Scanner(new File("Dates.txt"));
while(s.hasNext())
{
dates.put(LocalDate.parse("mm.dd.yyyy"), Integer);
}
System.out.println(dates);
}
}
我无法弄清楚在我的 dates.put(key,value) 代码行中放入什么内容,因此 HashMap 的格式如上。我不确定将 txt 文件读入 ArrayList 然后使用 put() 填充 HashMap 是否会更好。任何指导或答案将不胜感激!