gpt4 book ai didi

java - Pairs 的数据结构,其中每个值(成对)映射到其他值?

转载 作者:搜寻专家 更新时间:2023-10-31 08:23:47 29 4
gpt4 key购买 nike

我带着类似的问题又回来了。是否有可以返回其特定合作伙伴的数据类型?例如:

ExampleType<String,String> test = new ExampleType<String,String>();
test.put("hello","hi");

如果我输入 test.get("hi"),它会返回“hello”,如果我输入 test.get("hello"),它会返回“hi”。

我唯一的猜测可能是一个二维数组,但我不确定我将如何实现它。截至目前,我能理解的唯一方法是创建两个不同的 HashMap 并交换每个 HashMap 中的键。 (显然这不是很有效/有效)。

感谢大家的帮助!

最佳答案

您可以使用 Guava's BiMap为了这。它还支持反向查找:

A bimap (or "bidirectional map") is a map that preserves the uniqueness of its values as well as that of its keys. This constraint enables bimaps to support an "inverse view", which is another bimap containing the same entries as this bimap but with reversed keys and values.

如果您已经依赖于 commons-collections,那么您也可以使用 BidiMap

关于java - Pairs 的数据结构,其中每个值(成对)映射到其他值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9882169/

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