java - 映射所需的行为类型

标签 java map

在 map 中我们有键值对。如果我们尝试在 map 中放置相同的键 2 次,它会产生错误。现在我也想要类似的值行为。简而言之,当我放置一个新的键值时进入 map 的键和值都应该是唯一的,否则应该通过异常, 我怎样才能做到这一点?

最佳答案

听起来你想要一个 Guava 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.

(顺便说一句,Guava 总体上是一个很棒的库。非常值得使用。)

关于java - 映射所需的行为类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3477183/

相关文章:

java - 当套接字被接受时,我怎样才能关闭这个对话框?

c++ - error C2678 : binary '<' : no operator found which takes a left-hand operand. ..(或者没有可接受的转换)

java - 无法写入 JSON : No serializer found for class org. hibernate.proxy.pojo.javassist.JavassistLazyInitializer

java - 编写一个程序,在给定以下条件的情况下计算所有数字的平方和?

c# - 量化垃圾收集与显式内存管理的性能

Java:通用过滤器映射函数

java - 如何在 Java 中增加 Map 中特定键的值?

java - Map.Entry<K,V> 的比较器

java - 如何使用 jxl 逐张读取 excel 文件

java - 为什么我的字符串不能正确输出我的值?