The following libraries can be used when you want to refer to map from both key and value.
<!-- 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
<!-- 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