Java 映射,从值到键

发布于 2024-11-01 05:54:57 字数 63 浏览 0 评论 0原文

有什么方法可以获取与映射中已知值关联的键吗?通常你知道键并且想要获取值,但我想做相反的事情,从值到键。是否可以?

Is there any way to get a key associated with a known value in a map? Normally you know the key and you want to get the value, but I want to do the opposite, going from value to key. Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

很糊涂小朋友 2024-11-08 05:54:57

是的,您必须迭代映射中的值,然后将每个键存储在列表中:

for (Map.Entry<K,V> entry : map.entrySet()) {
  V value = entry.getValue();
  if (value.equals(someTargetValue) {
      // add key (entry.getKey()) to list
  }
}

或者您可以使用 双向地图,但请注意:

此映射强制限制键和值之间存在 1:1 关系,这意味着多个键不能映射到同一值。

Yes, you have to iterate over the values in the map and then store each key in a list:

for (Map.Entry<K,V> entry : map.entrySet()) {
  V value = entry.getValue();
  if (value.equals(someTargetValue) {
      // add key (entry.getKey()) to list
  }
}

Or you could use a bidirectional map, though do note:

This map enforces the restriction that there is a 1:1 relation between keys and values, meaning that multiple keys cannot map to the same value.

不寐倦长更 2024-11-08 05:54:57

好吧,我不是 Google 项目 LambdaJ 方面的专家,但它确实提供了一些很酷的替代方案。

假设您有一张包含该月所有日期的地图:

month.put(1,"Monday");
month.put(2,"Tuesday");
month.put(3,"Wednesday");
...

那么我们可以轻松实现您想要的目标,如下所示:

Set<Integer> result = with(month).retainValues(is("Friday")).keySet();

或者甚至是一些更有趣的搜索,例如:

Set<Integer> result = with(month).retainValues(anyOf(is("Monday"),is("Friday"))).keySet();

Well, I am not an expert on Google Project LambdaJ, but it certainly offers a few cool alternatives.

Supposing you have a map with all the days of the month:

month.put(1,"Monday");
month.put(2,"Tuesday");
month.put(3,"Wednesday");
...

Then we could easily achieve what you want like this:

Set<Integer> result = with(month).retainValues(is("Friday")).keySet();

Or even a few more interesting searches like:

Set<Integer> result = with(month).retainValues(anyOf(is("Monday"),is("Friday"))).keySet();
你的他你的她 2024-11-08 05:54:57

无需迭代所有键来查找值,您可以使用 Apache Commons BidiMap

Without iterating all the keys looking for the value you can use an Apache Commons BidiMap

枫林﹌晚霞¤ 2024-11-08 05:54:57

映射是一个数学条目,并不意味着可以进行反向映射。也就是说,如果每个映射值都是唯一的,您也许能够创建“反向”映射。当然,您必须将所有数据操作封装在适当更新两个映射的方法中。

Map<Key, Value> normal;
Map<Value, Key> reverse;

如果每个映射值都不唯一,那么您需要创建值到键列表的反向映射。

Map<Key, Value> normal;
Map<Value, List<Key>> reverse;

最后,如果您不关心快速访问,您可以迭代整个 Map 来查找值。由于您需要值和键,因此最好迭代 Map.Entry 项。

Value searchingFor = ...;
Map<Key, Value> normal;
List<Key> keys = new ArrayList<Key>();
for (Map.Entry<Key, Value> entry : normal.entrySet()) {
   if (entry.getValue().equals(searchingFor)) {
     keys.add(entry.getKey());
   }
}

您选择使用的技术在很大程度上取决于以速度换取内存占用是否更好。通常,由于对值进行哈希处理,因此拥有额外的映射会更快,但会消耗额外的内存。对 Map.Entry 进行循环速度较慢,但​​占用的内存较少。

A Map is a mathematical entry which doesn't imply that a reverse mapping is possible. That said you might be able to create a "reverse" mapping if every mapped value is unique. Naturally, you'll have to encapsulate all the data manipulations in methods that update both Maps appropriately.

Map<Key, Value> normal;
Map<Value, Key> reverse;

If every mapped value is not unique, then you need to create a reverse mapping of a value to a list of keys.

Map<Key, Value> normal;
Map<Value, List<Key>> reverse;

Finally, if you don't care about fast access, you can iterate over the entire Map looking for Values. Since you'll need both the Value and the Key, it is probably best to iterate over the Map.Entry items.

Value searchingFor = ...;
Map<Key, Value> normal;
List<Key> keys = new ArrayList<Key>();
for (Map.Entry<Key, Value> entry : normal.entrySet()) {
   if (entry.getValue().equals(searchingFor)) {
     keys.add(entry.getKey());
   }
}

The technique you choose to use will depend heavily on whether it is better to trade speed for memory footprint. Generally having an extra Map is faster due to the hashing being done on the Value(s), but costs extra memory. Having a loop over the Map.Entry(s) is slower, but costs less memory.

睫毛上残留的泪 2024-11-08 05:54:57

在这里他们已经讨论了双向地图。如今,Guava (https://github.com/google/guava) 提供了一个很好的 BiMap,您可以使用它可以用于此目的:

https://github.com/google/guava/wiki/ NewCollectionTypesExplained#bimap

Here they already talked about Bidirectional maps. Nowadays, Guava (https://github.com/google/guava) offers a nice BiMap that you can use for that purpose:

https://github.com/google/guava/wiki/NewCollectionTypesExplained#bimap

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文