繁体   English   中英

Java - 从文本文件中读取并将 item( string , arrayList) 添加到 hashmap

[英]Java -read from a text file and Add item( string , arrayList) to hashmap

这是文本文件:output1.txt

zzz ***Wed Jan 15 10:00:03 +08 2020
a : 20
b : 30
c : 40
zzz ***Wed Jan 15 11:00:03 +08 2020
a : 22
b : 24
c : 25

我正在尝试将 String 中的日期和 a,b,c 作为ArrayList到哈希映射中:

期望的输出:

{zzz ***Wed Jan 15 10:00:03 +08 2020=[a : 20, b : 30, c : 40],
 zzz ***Wed Jan 15 11:00:03 +08 2020=[a : 22, b : 24, c : 25]}

我的代码:

    String dateString ="";
    ArrayList<String> value = new ArrayList<String>();
    HashMap<String, ArrayList> result = new HashMap<String, ArrayList>();

    String fileName = "/Users/--/Downloads/output1.txt";
    File file = new File(fileName);
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    String line;

    while ((line = br.readLine()) != null) {
        if (line.startsWith("zzz")) {
            dateString = line;
        } else {
            value.add(line);
        }
        result.put(dateString, value);
    }
    System.out.println(result);

我得到的结果是:

{zzz ***Wed Jan 15 10:00:03 +08 2020=[a : 20, b : 30, c : 40, a : 22, b : 24, c : 25], 
 zzz ***Wed Jan 15 11:00:03 +08 2020=[a : 20, b : 30, c : 40, a : 22, b

这是示例代码。

我们使用Scanner来解析输入字符串的每一行。 扫描器对象在try-with-resources 中定义,以自动关闭。

我们使用DateTimeFormatter定义日期时间格式模式以匹配您的时髦输入。 提示:向数据发布者介绍ISO 8601标准,该标准定义了将日期时间值作为文本交换的实用格式。

作为我们的关键,我们解析每四行中的第一行以获得一个OffsetDateTime对象。 这是我们Map的关键。 我们的地图具体定义为TreeMap ,以按时间顺序排列键。

我们构建了一个从预期的“a、b、c”值解析出来的Integer List 在构建未知具体类型的List对象时,我们使用新的List.of功能来简化语法。 List将元素按照添加的顺序保存,所以我们知道第一个是a ,第二个是b ,第三个是c

String s = "zzz ***Wed Jan 15 10:00:03 +08 2020\n" +
        "a : 20\n" +
        "b : 30\n" +
        "c : 40\n" +
        "zzz ***Wed Jan 15 11:00:03 +08 2020\n" +
        "a : 22\n" +
        "b : 24\n" +
        "c : 25";

DateTimeFormatter f = DateTimeFormatter.ofPattern( "'zzz ***'EEE MMM dd HH:mm:ss x uuuu" ).withLocale( Locale.US );
Map < OffsetDateTime, List < Integer > > momentCounts = new TreeMap <>();
try (
        Scanner scanner = new Scanner( s ) ;
)
{
    while ( scanner.hasNextLine() )
    {
        OffsetDateTime moment = OffsetDateTime.parse( scanner.nextLine() , f );
        List < Integer > counts = List.of(
                Integer.valueOf( scanner.nextLine().replace( "a : " , "" ) ) ,
                Integer.valueOf( scanner.nextLine().replace( "b : " , "" ) ) ,
                Integer.valueOf( scanner.nextLine().replace( "c : " , "" ) )
        );
        momentCounts.put( moment , counts );
    }
}

System.out.println( "momentCounts = " + momentCounts );

跑的时候。

momentCounts = {2020-01-15T10:00:03+08:00=[20, 30, 40], 2020-01-15T11:00:03+08:00=[22, 24, 25]}

在阅读以“zzz”开头的新行之前,只需初始化新数组。

String fileName = "/Users/--/Downloads/output1.txt";
File file = new File(fileName);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;

while ((line = br.readLine()) != null) {
    if (line.startsWith("zzz")) {
        dateString = line;
        value = new ArrayList<>();
    } else {
        value.add(line);
    }
    result.put(dateString, value);
}
System.out.println(result);

暂无
暂无

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

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