gpt4 book ai didi

java - Java 中用于重复键值对的适当集合类

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:25:28 26 4
gpt4 key购买 nike

我应该为以下数据使用哪个集合类

(key1, value1)
(key1, value2)
(key2, value3)
(key3, value3)

键和值不不同

最佳答案

您可以滚动自己的实现:

Map<Key, List<Value>> 

或使用 Multimap from Guava ,在他们的文档中,他们将其解释为:

There are two ways to think of a Multimap conceptually: as a collection of mappings from single keys to single values:

a -> 1 a -> 2 a -> 4 b -> 3 c -> 5

or as a mapping from unique keys to collections of values:

a -> [1, 2, 4] b -> 3 c -> 5

以下blog post解释了使用 Guava 集合的一些优势。

还有 MultiValueMap from the Apache Commons Collections如果您对外部库有偏好,这可以解决同样的问题。

关于java - Java 中用于重复键值对的适当集合类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22141178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com