1 Introduction
We all know that Map is a container for storing Key Value pairs < Key, Value > and that the Key Value can be obtained quickly by using Map.get(key). However, sometimes we need to get the Value and find the Key.
In this paper, four methods are introduced with an example. The Key Value is obtained by passing in the Value value Value.
2 four methods
2.1 circulation method
Circular method is to find out the qualified ones by traversing the entries in the Map and comparing them one by one. There are three situations:
- (1) find a value
- (2) multiple values found
- (3) not found
The specific code is as follows:
@Test public void loop() { Map<String, Integer> map = ImmutableMap.of("A", 1, "B", 2, "C", 3, "D", 2); //Found a value assertEquals("A", getKeyByLoop(map, 1)); //Multiple values found assertEquals(ImmutableSet.of("B", "D"), getKeysByLoop(map, 2)); //Can't find assertEquals(null, getKeyByLoop(map, 4)); } private <K, V> K getKeyByLoop(Map<K, V> map, V value) { for (Map.Entry<K, V> entry : map.entrySet()) { if (Objects.equals(entry.getValue(), value)) { return entry.getKey(); } } return null; } private <K, V> Set<K> getKeysByLoop(Map<K, V> map, V value) { Set<K> set = Sets.newHashSet(); for (Map.Entry<K, V> entry : map.entrySet()) { if (Objects.equals(entry.getValue(), value)) { set.add(entry.getKey()); } } return set; }
In particular, the Objects.equals(a, b) method is used in comparison with the a.equals(b) method. This avoids null pointer exceptions.
2.2 Stream method
Stream can always provide elegant and intuitive methods in a variety of collection operations, easy to write and understand. Through a filter, the values that meet the equal conditions can be taken out. The code is as follows:
@Test public void stream() { Map<String, Integer> map = ImmutableMap.of("A", 1, "B", 2, "C", 3, "D", 2); assertEquals(ImmutableSet.of("B", "D"), getKeysByStream(map, 2)); } private <K, V> Set<K> getKeysByStream(Map<K, V> map, V value) { return map.entrySet() .stream() .filter(kvEntry -> Objects.equals(kvEntry.getValue(), value)) .map(Map.Entry::getKey) .collect(Collectors.toSet()); }
2.3 BiMap of guava
Google's Guava provides BiMap, a two-way Map. Calling the inverse() method will return a reverse associated BiMap, and then you can get the key value through the get() method.
The code is as follows:
@Test public void guava() { BiMap<String, Integer> biMap = HashBiMap.create(); biMap.put("A", 1); biMap.put("B", 2); biMap.put("C", null); biMap.put("D", 4); assertEquals("D", biMap.inverse().get(4)); }
It should be noted that BiMap, as a two-way Map, cannot store many to one relationships; however, HashMap can. In fact, it's easy to understand. Because it's bidirectional, it's necessary to satisfy the uniqueness of both the Key Value and the Value value. If the same Value is stored in, an exception will be thrown: java.lang.IllegalArgumentException: value already present.
2.4 BidiMap of Apache commons collections
Similarly, Apache Commons Collections also provides BidiMap, a bi-directional Map class. It also maintains a one-to-one relationship, not many to one. It provides the getKey(value) method to return the Key value. The code is as follows:
@Test public void apacheCommons() { BidiMap<String, Integer> bidiMap = new DualHashBidiMap<>(); bidiMap.put("A", 1); bidiMap.put("B", 2); bidiMap.put("C", null); bidiMap.put("D", 4); assertEquals("D", bidiMap.getKey(4)); }
Different from Guava's BiMap, when the same Value is stored, it will not throw an exception, but overwrite the original data.
3 Summary
This paper introduces four methods to get the Key Value of Map through Value value Value, which are loop method, Stream method, Guava method and Apache commons collection method. These four methods are similar but different.
- (1) the loop method and the use of Stram are essentially traversal. If a Map often needs to take the Key value in reverse, it is not recommended to use it. You can consider the two-way Map provided by Guava and Apache Commons;
- (2) bidirectional Map is actually a space for time idea. Although it can find the Key value that meets the conditions quickly, it also uses more space to store bidirectional Map;
- (3) two way Map does not support many to one relationship.
How to choose depends on the specific needs.
Welcome to the public number "pumpkin slow talk", which will continue to update for you.
Read more, share more; write more, organize more.