Multimap实现具有重复键

huangapple go评论77阅读模式
英文:

Multimap implementation with duplicate keys

问题

以下是翻译好的部分:

我有一个函数以以下方式返回一个数组列表

[java.lang.String=name, int=parameters, byte=paramOne, java.lang.String=format, byte=paramTwo]

并存储在一个变量中如下所示:`List<Entry<String, String>> dataTypes`

现在我想创建一个映射以存储类似于这个列表的键值对我尝试过制作键和值的单独列表然后尝试使用multimap将它们相互映射但是没有成功。(我知道hashmap不能存储重复的键并且只会取最后一个重复的键及其相应的值这就是为什么我尝试使用multimaps的原因
这是我尝试的代码

List<String> dataTypeValues = new ArrayList<>();
List<String> dataTypeKeysList = new ArrayList<>();
String keyString = null;
String valueString = null;

for(Entry<String, String> dt : dataTypes) {
    dataTypeKeys.add(dt.getKey());
    keyString = dataTypeKeys.toString();
}

for(Entry<String, String> dt : dataTypes) {
    dataTypeValues.add(dt.getValue());
    valueString = dataTypeValues.toString();
}
Multimap<String, String> multiMap = ArrayListMultimap.create();
multiMap.put(keyString, valueString);
System.out.println(multiMap);

这是我得到的输出

{[java.lang.String, int, byte, java.lang.String, byte]=[[name, parameters, paramOne, format, paramTwo]]}

这是我想要的输出

{java.lang.String=[name], int=[parameters], byte=[paramOne,paramTwo], java.lang.String=[format]}

任何建议将非常有帮助提前感谢

编辑:由**@User - Upvote don't say Thanks@Louis Wasserman提供的答案完全有效。我接受了@User - Upvote don't say Thanks**的答案,因为有讨论。如果可以的话,我会接受这两个答案。谢谢!

英文:

I have a function which returns an array list in the following way

[java.lang.String=name, int=parameters, byte=paramOne, java.lang.String=format, byte=paramTwo]

and is stored in a variable like : List<Entry<String, String>> dataTypes

Now I want to make a map that stores the key-value pairs like this list. I have tried to make separate lists of keys and values and I'm trying to use a multimap to map it against each other but it does not work. (I know hashmaps can't store duplicate keys and will just take the last duplicate key with its respective value, that's why I'm trying to use multimaps)
This is the code I'm trying:

        List<String> dataTypeValues = new ArrayList<>();
List<String> dataTypeKeysList = new ArrayList<>();
String keyString = null;
String valueString = null;
for(Entry<String, String> dt : dataTypes) {
dataTypeKeys.add(dt.getKey());
keyString = dataTypeKeys.toString();
}
for(Entry<String, String> dt : dataTypes) {
dataTypeValues.add(dt.getValue());
valueString = dataTypeValues.toString();
}
Multimap<String, String> multiMap = ArrayListMultimap.create();			
multiMap.put(keyString, valueString);
System.out.println(multiMap);

And this is the output I'm getting:

{[java.lang.String, int, byte, java.lang.String, byte]=[[name, parameters, paramOne, format, paramTwo]]}    

And this is the output I'm trying to get:

{java.lang.String=[name], int=[parameters], byte=[paramOne,paramTwo], java.lang.String=[format]}

Any advice would be really helpful. Thanks in advance!

EDIT : The answers provided by @User - Upvote don't say Thanks and @Louis Wasserman works perfectly. I accepted the answer from @User - Upvote don't say Thanks because of the discussion. If I could I would accept both these answers.
Thank you

答案1

得分: 2

你可以使用 groupingBy,我认为这对你来说是理想情况

Map<String, List<String>> res =
dataTypes.stream().collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
英文:

You can use groupingBy which is the ideal case for you I think

Map<String, List<String>> res =
dataTypes.stream().collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));

答案2

得分: 1

获取最后一个重复的键

这正是 Collectors.toMap 的确切目的。

Map<String, List<String>> multiMap = dataTypes.stream().collect(
    toMap(
        Map.Entry::getKey,
        e -> singletonList(e.getValue()),
        (v1, v2) -> {
            List<String> values = new ArrayList<>();
            values.addAll(v1);
            values.addAll(v2);

            return values;
        }
    )
)
英文:

> take the last duplicate key

That is the exact purpose of Collectors.toMap.

Map&lt;String, list&lt;String&gt; multiMap = dataTypes.stream.collect(toMap(
Map.Entry::getKey,
e -&gt; singletonList(e.getValue()),
(v1, v2) -&gt; {
List&lt;String&gt; values = new ArrayList&lt;&gt;();
values.addAll(v1);
values.addAll(v2);
return values;
}
))

答案3

得分: 1

不清楚您为什么要这样将条目放入多重映射中。至少,您想要的是:

for (Entry<String, String> dt : dataTypes) {
  multiMap.put(dt.getKey(), dt.getValue());
}

然后,您可能想要打印multimap.entries()。如果您关心条目的顺序,您可能需要使用LinkedListMultimap

英文:

It's not clear to me why you're trying to put the entries into the multimap like that. At minimum, what you want is

for (Entry&lt;String, String&gt; dt : dataTypes) {
multiMap.put(dt.getKey(), dt.getValue());
}

Then, you probably want to print multimap.entries(). And if you care about the order of the entries, you probably want LinkedListMultimap.

huangapple
  • 本文由 发表于 2020年8月8日 02:43:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63307588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定