简体   繁体   English

如何使用 txt 文件中的数据填充 HashMap

[英]How to populate a HashMap with data from a txt file

I need to put the following dates from Dates.txt file into a HashMap.我需要将 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

The format for the HashMap should be HashMap 的格式应该是

Initial 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

My code snippet so far到目前为止我的代码片段

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);
    }
}

I am having trouble figuring out what to put in my dates.put(key,value) line of code so the HashMap is formatted as above.我无法弄清楚在我的dates.put(key,value) 代码行中放入什么,因此HashMap 的格式如上。 I'm not sure if it'd be better to read the txt file into an ArrayList then using put() to populate the HashMap.我不确定将 txt 文件读入 ArrayList 然后使用 put() 填充 HashMap 是否更好。 Any guidance or answers would be appreciated!任何指导或答案将不胜感激!

You can do it as follows:你可以这样做:

Map<LocalDate, Integer> map = new HashMap<>();
Scanner s = new Scanner(new File("Dates.txt"));
while (s.hasNextLine()) {
    // Read a line
    String line = s.nextLine();
    // Split the line on whitespace
    String[] data = line.split("\\s+");
    if (data.length == 2) {
        map.put(LocalDate.parse(data[1], DateTimeFormatter.ofPattern("MM.dd.uuuu")), Integer.valueOf(data[0]));
    }
}
System.out.println(map);

An example of parsing the date string in the given pattern:解析给定模式中的日期字符串的示例:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.parse("05.05.2017", DateTimeFormatter.ofPattern("MM.dd.uuuu"));
        System.out.println(date);
    }
}

Output:输出:

2017-05-05

Learn more about the modern date-time API at Trail: Date Time .Trail: Date Time 中了解有关现代日期时间 API 的更多信息。

Just as an alternative to the excellent answer already provided, this could be done by using streams:正如已经提供的优秀答案的替代方案一样,这可以通过使用流来完成:

Map<LocalDate, Integer> result = Files.lines(Paths.get("Dates.txt")
    .map(l -> l.split(" ")).filter(fs -> fs.length == 2)
    .collect(Collectors.toMap(
         fs -> LocalDate.parse(fs[1], DateTimeFormatter.ofPattern("MM.dd.uuuu")), 
         fs -> Integer.valueOf(fs[0]));

From the given data, it looks like there's no really reason to use a map - as you noted, you've struggled to find an appropriate value.从给定的数据来看,似乎没有真正的理由使用地图 - 正如您所指出的,您一直在努力寻找合适的值。 You could accumulate them in to an ArrayList , or even a HashSet if the order isn't important instead.如果顺序不重要,您可以将它们累积到ArrayList或什至HashSet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM