Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在HashMap中为同一个键存储多个值?_Java_Hashmap - Fatal编程技术网

Java 如何在HashMap中为同一个键存储多个值?

Java 如何在HashMap中为同一个键存储多个值?,java,hashmap,Java,Hashmap,我有一个名为InputRow的字符串,如下所示: 1,Kit,23 2,Ret,211 我在上面应用Regex.+,.+,.+并将结果存储在多个变量中 对于第一行,kit,23,我得到: 对于第二行2,Ret,211,我得到: 我想用同一个键InputRow将所有输入行存储在HashMap中。我如何在Java中做到这一点 我的Java代码是 line="1,Kit,23"; final Map<String, String> regexResults = new HashMap&l

我有一个名为InputRow的字符串,如下所示:

1,Kit,23
2,Ret,211
我在上面应用Regex.+,.+,.+并将结果存储在多个变量中

对于第一行,kit,23,我得到:

对于第二行2,Ret,211,我得到:

我想用同一个键InputRow将所有输入行存储在HashMap中。我如何在Java中做到这一点

我的Java代码是

line="1,Kit,23";
final Map<String, String> regexResults = new HashMap<>();
Pattern pattern = Pattern.compile("(.+),(.+),(.+)");
final Matcher matcher = pattern.matcher(line);
if (matcher.find()) 
{
final String baseKey = "InputRow";
for (int i = 0; i <= matcher.groupCount(); i++) {
final String key = new StringBuilder(baseKey).append(".").append(i).toString();
 String value = matcher.group(i);
if (value != null) {
   regexResults.put(key, value);
}
}

现在我想将第二行也存储在要处理的regexResults中。这是怎么可能的?

这是不可能的。一个映射只能为每个定义存储一个键。文件上说

将键映射到值的对象。地图不能包含重复的键;每个键最多可以映射到一个值

您唯一能做的就是将键映射到值列表。地图然后它可能看起来像这样

InputRow.1 --> [1, kit, 23],
InputRow.2 --> [2, Ret, 211]
在InputRow中创建一个类:

还有一个HashMap。哈希映射键是您的行索引,您可以将所有匹配行作为列表分配给哈希映射

为了澄清,HashMap为一个唯一键存储一个条目。因此,不能将多个条目分配给同一个键,否则该条目将被覆盖。因此,您需要编写一个容器来覆盖多个对象,或者使用现有的like列表

代码示例

我使用了两个文本片段,由换行符分隔,所以有两行。此代码段将列表中的两个InputRow对象放入带有键InputRow的HashMap中。请注意,匹配器组索引从1开始,零表示整个组。还要注意,为了简单起见,我假设您创建了一个InputRowString、String、String构造函数

String line = "1,Kit,23\n2,Ret,211";

final Map<String, List<InputRow>> regexResults = new HashMap<>();
Pattern pattern = Pattern.compile("(.+),(.+),(.+)");
final Matcher matcher = pattern.matcher(line);

List<InputRow> entry = new ArrayList<>();

while (matcher.find()) {
    entry.add(new InputRow(matcher.group(1), matcher.group(2), matcher.group(3)));
}

regexResults.put("InputRow", entry);

当然不确定您的示例显示了什么,但是使用保存这些字段的自定义类可以存储多个键。发布您到目前为止所做的工作。与我们共享您的代码,但是首先阅读hashCode和equals方法,它们应该可以帮助您在指定的class@Jobin我已经发布了我的代码,其中triedStill,您不能为一个键存储多个条目,因为这里已经回答了多次,唯一的可能性是使用类似容器的列表,正如我在下面的回答中所描述的。
InputRow.1 --> [1, kit, 23],
InputRow.2 --> [2, Ret, 211]
class InputRow {

    private int value1;
    private String value2;
    private int value3;

    //...getters and setters

}
String line = "1,Kit,23\n2,Ret,211";

final Map<String, List<InputRow>> regexResults = new HashMap<>();
Pattern pattern = Pattern.compile("(.+),(.+),(.+)");
final Matcher matcher = pattern.matcher(line);

List<InputRow> entry = new ArrayList<>();

while (matcher.find()) {
    entry.add(new InputRow(matcher.group(1), matcher.group(2), matcher.group(3)));
}

regexResults.put("InputRow", entry);