【发布时间】:2019-09-30 19:48:29
【问题描述】:
Java 中是否有数据结构(java util、guava...)允许我存储也可以解释为值键的“键值”对?
例子:
Datastructure d = new Datastructure();
d.add(1, "foo");
d.add(21 "bar");
d.add(33 "hello");
d.add(55 "world");
像d.get1(1) 这样的函数应该返回foo。
像d.get2("foo") 这样的函数应该返回1。
像d.get1(33) 这样的函数应该返回hello。
像d.get2("hello") 这样的函数应该返回33。
...
有这样的东西吗?
【问题讨论】:
-
Key 可以是唯一的,但 value 不能是唯一的,那么在这种情况下,您将如何通过 value 获取 Key ?到目前为止,Java 中还没有这样的功能。
-
根据您所写的内容,您只需要一个 Map(例如 HashMap)
-
嘿@Adam,你将如何在java中通过get(value)获取key。我知道有map。
标签: java dictionary data-structures key bimap