Java 如何使用HashMap将多个值放入ArrayList?

Java 如何使用HashMap将多个值放入ArrayList?,java,arrays,hashmap,Java,Arrays,Hashmap,我收到了JSON对象,并对其进行了解析 数据如下所示 [Parsed_showinfo_2 @ 0x9b4da80] n:88 pts:17425897 pts_time:726.079 pos:149422375 fmt:rgb24 sar:405/404 s:640x360 i:P iskey:1 type:I checksum:48E13810 plane_checksum:[48E13810] [Parsed_showinfo_2 @ 0x9b4da80] n:89 pts:174719

我收到了JSON对象,并对其进行了解析

数据如下所示

[Parsed_showinfo_2 @ 0x9b4da80] n:88 pts:17425897 pts_time:726.079 pos:149422375 fmt:rgb24 sar:405/404 s:640x360 i:P iskey:1 type:I checksum:48E13810 plane_checksum:[48E13810]
[Parsed_showinfo_2 @ 0x9b4da80] n:89 pts:17471943 pts_time:727.998 pos:149646339 fmt:rgb24 sar:405/404 s:640x360 i:P iskey:0 type:P checksum:1AAD40E0 plane_checksum:[1AAD40E0]
[Parsed_showinfo_2 @ 0x9b4da80] n:90 pts:17503975 pts_time:729.332 pos:149806608 fmt:rgb24 sar:405/404 s:640x360 i:P iskey:1 type:I checksum:3DA5F0DB plane_checksum:[3DA5F0DB]
然后,我使用JSONParser()解析数据,因为我需要pts,pts_时间值

FileInputStream fstream = new FileInputStream("myfile");
DataInputStream in = new DataInputStream(fstream);
BufferedReader buff = new BufferedReader(new InputStreamReader(in));

//Which type put to ?
HashMap<String, ?> map = new HashMap<String, ?>();
//Should I use ArrayList too?
//ArrayList<HashMap<String, ?>> list = new ArrayList<HashMap<String, ?>>();

try {
    while (strLine = buff.readLine()) != null) {
        s = strLine.split(" ");
        pts = Long.parseLong(s[4].split(":")[1]);
        ptstime = s[5].split(":")[1];
    }
} catch (IOException ex) { }
pts
ptstime
是键


如何在while循环中进行编码

以下是您的方法:

Map<String, List<Object>> map = new HashMap<>();
...
while (strLine = buff.readLine()) != null) {
    s = strLine.split(" ");
    pts = Long.parseLong(s[4].split(":")[1]);
    List<String> listPts = map.get("pts");
    if (listPts == null) {
        listPts = new ArrayList<>();
        map.put("pts", listPts);
    }
    listPts.add(pts);
    // Here apply the same logic to the rest
}

以下是您如何做到这一点:

Map<String, List<Object>> map = new HashMap<>();
...
while (strLine = buff.readLine()) != null) {
    s = strLine.split(" ");
    pts = Long.parseLong(s[4].split(":")[1]);
    List<String> listPts = map.get("pts");
    if (listPts == null) {
        listPts = new ArrayList<>();
        map.put("pts", listPts);
    }
    listPts.add(pts);
    // Here apply the same logic to the rest
}

DataInputStream
的作用是什么?当您将其用作
InputStreamReader
的参数时,
FileInputStream
应该已经很好了

至于你的问题,我想你想要的是

Map<String, List<?>> map = new HashMap<>(2);
map.put("pts", new ArrayList<Long>());
map.put("ptstime", new ArrayList<Long>());

DataInputStream
的作用是什么?当您将其用作
InputStreamReader
的参数时,
FileInputStream
应该已经很好了

至于你的问题,我想你想要的是

Map<String, List<?>> map = new HashMap<>(2);
map.put("pts", new ArrayList<Long>());
map.put("ptstime", new ArrayList<Long>());

JSON对象在哪里?在哪里调用JSON解析器?你在从文件中读什么?为什么要通过
DataInputStream
?您只需将值添加到循环后的哈希映射中,JSON对象在哪里?在哪里调用JSON解析器?你在从文件中读什么?为什么要通过
DataInputStream
?您只需在循环后将值添加到哈希映射中,
map.get("pts").add(pts.toString());
map.get("ptstime").add(ptstime);