Java bidirectional map library

The following libraries can be used when you want to refer to map from both key and value.

Guava BiMap

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>22.0</version>
</dependency>
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;

Map<String, String> m = new HashMap<>();
m.put("a", "Ah");
m.put("i", "I");
m.put("u", "U");

BiMap<String, String> bimap = HashBiMap.create(m);
System.out.println(bimap.get("a"));//Ah
System.out.println(bimap.inverse().get("Ah"));//a

BidiMap from Apache Commons Collections

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.1</version>
</dependency>
import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.bidimap.DualHashBidiMap;

BidiMap<String, String> bidimap = new DualHashBidiMap<>(m);
System.out.println(bidimap.get("a"));//Ah
System.out.println(bidimap.inverseBidiMap().get("Ah"));//a

Recommended Posts

Java bidirectional map library
JAVA (Map)
[Java] Map comparison
Java coverage measurement library
[Java] Stream API / map
Enum reverse map Java
Java CSV library "opencsv"
Java tree structure library
[Java] How to use Map
[Java] How to use Map
[Design pattern] Java core library
How to use Java Map
[Java] Stream (filter, map, forEach, reduce)
Get Null-safe Map values in Java
[Java] Convert 1-to-N List to Map
Java
Try bidirectional communication with gRPC Java
Java
Java8 list conversion with Stream map
[Java] Get List / Map elements with Iterator
Duplicate Map sorted by key in Java
Call Java library from C with JNI
[Beginner] Java method / class / external library [Note 23]
Reverse Key from Value in Java Map
Study Java Try using Scanner or Map
[Java] Use cryptography in the standard library
2018 Java Recommended library for easily creating microservices
[Java] Map # merge is hard to understand.
[Java] Let's make a DB access library!