package org.example.testBiMap;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Maps;
public class Main {
public static void main(String[] args) {
BiMap<Integer, String> biMap = Maps.synchronizedBiMap(HashBiMap.create());
biMap.put(1, "a");
System.out.println(biMap.get(1));
BiMap<String, Integer> inverse = biMap.inverse();
System.out.println(inverse.get("a"));
System.out.println(inverse.get("b"));
// 必须要求value唯一,之前的已经存在过
// biMap.put(12, "a");
biMap.forcePut(12, "a");
System.out.println(biMap.get(12));
System.out.println(biMap.inverse().get("a"));
}
}
/*
a
1
null
a
12
*/
理解:我觉得还是用forcePut比较好。