RangeMap - 范围Map Multimap - 多值Map BiMap - 双向Map : 根据value查找对应的key ImmutableMap -创建不可变Map初始化后,不能在更改 SortedMap- 创建一个有序map
Multimap | 实现 key 使用的是 | value 使用的是 |
---|---|---|
ArrayListMultimap | HashMap | ArrayList |
HashMultimap | HashMap | HashSet |
LinkedListMultimap * | LinkedHashMap* | LinkedList* |
LinkedHashMultimap** | LinkedHashMap | LinkedHashSet |
TreeMultimap | TreeMap | TreeSet |
ImmutableListMultimap | ImmutableMap | ImmutableList |
ImmutableSetMultimap | ImmutableMap | ImmutableSet |
在线演示源码: | ||
https://github.com/wangmaoxiong/apache-study/blob/master/src/main/java/com/wmx/guava/MultimapTest.java |
■ 构建map
// 构造一个可变的空的HashMap实例
HashMap hashMap1 = Maps.newHashMap();
// 构造一个与给定map有相同映射关系的可变HashMap实例
HashMap hashMap2 = Maps.newHashMap(new HashMap<>());
// 构造一个期望长度为expectedSize的HashMap实例.
HashMap hashMap3 = Maps.newHashMapWithExpectedSize(23);
// 构造一个具有给定键类型的EnumMap实例
EnumMap enumMap1 = Maps.newEnumMap(MapEnum.class);
// 构造一个与给定map有相同映射关系的EnumMap实例
EnumMap enumMap2 = Maps.newEnumMap(new EnumMap<>(MapEnum.class));
// 构造一个可变的、空的TreeMap实例
TreeMap treeMap1 = Maps.newTreeMap();
// 构造一个可变的、空的、使用给定比较器的TreeMap实例
TreeMap treeMap3 = Maps.newTreeMap(Comparator.comparingInt(Object::hashCode));
// 构造一个可变的、与给定SortedMap有相同映射关系的TreeMap实例
TreeMap treeMap2 = Maps.newTreeMap(new TreeMap<>());
// 构造一个可变的、空的、插入排序的LinkedHashMap实例
LinkedHashMap linkedHashMap1 = Maps.newLinkedHashMap();
// 构造一个可变的、插入排序的、与给定map有相同映射关系的LinkedHashMap实例
LinkedHashMap linkedHashMap2 = Maps.newLinkedHashMap(new LinkedHashMap<>());
// 构造一个期望长度为expectedSize的LinkedHashMap实例
LinkedHashMap linkedHashMap3 = Maps.newLinkedHashMapWithExpectedSize(23);
// 构造一个ConcurrentMap实例
ConcurrentMap<Object, Object> concurrentHashMap1 = Maps.newConcurrentMap();
// 构造一个IdentityHashMap实例
IdentityHashMap identityHashMap1 = Maps.newIdentityHashMap();
■ RangeMap 类 RangeMap 是一种集合类型(collection type),它将不相交、且不为空的 Range(key)映射给一个值(Value)。和 RangeSet不一样,RangeMap 不可以将相邻的区间合并,即使这个区间映射的值是一样的。
和 RangeSet 一样,实现 RangeMap 也是一个接口,实现它的也只有两个类,分别为 mmutableRangeMap 和 TreeRangeMap。用的多的还是 TreeRangeMap,下面主要以 TreeRangeMap 来说明 RangeMap。 具体参考:https://google.github.io/guava/releases/27.0.1-jre/api/docs/com/google/common/collect/RangeMap.html
■ map之间的差异
MapDifference mapDifference1 = Maps.difference( // 返回两个给定map之间的差异
new HashMap<String, String>() {{
put("a", "a");
put("b", "1");
put("c", "c");
}},
new HashMap<String, String>() {{
put("a", "a");
put("b", "2");
put("d", "d");
}}
);
System.out.println(mapDifference1.entriesDiffering().entrySet()); // 输出: [b=(1, 2)]
System.out.println(mapDifference1.entriesInCommon().entrySet()); // 输出: [a=a]
System.out.println(mapDifference1.entriesOnlyOnLeft().entrySet()); // 输出: [c=c]
System.out.println(mapDifference1.entriesOnlyOnRight().entrySet()); // 输出: [d=d]
MapDifference mapDifference2 = Maps.difference( // 返回两个已排序的Map之间的差异, 通过给定left的比较器
new TreeMap<String, String>() {{
put("a", "a");
put("b", "1");
put("c", "c");
}},
new HashMap<String, String>() {{
put("a", "a");
put("b", "2");
put("d", "d");
}}
);
System.out.println(mapDifference2.entriesDiffering().entrySet()); // 输出: [b=(1, 2)]
System.out.println(mapDifference2.entriesInCommon().entrySet()); // 输出: [a=a]
System.out.println(mapDifference2.entriesOnlyOnLeft().entrySet()); // 输出: [c=c]
System.out.println(mapDifference2.entriesOnlyOnRight().entrySet()); // 输出: [d=d]
MapDifference mapDifference3 = Maps.difference( // 返回两个给定map之间的差异, 通过所提供的valueEquivalence进行等值比较
new HashMap<String, String>() {{
put("a", "a");
put("b", "1");
put("c", "c");
}},
new HashMap<String, String>() {{
put("a", "a");
put("b", "2");
put("d", "d");
}},
new Equivalence<String>() {
@Override
protected boolean doEquivalent(String a, String b) {
return a.hashCode() < b.hashCode();
}
@Override
protected int doHash(String s) {
return 0;
}
}
);
System.out.println(mapDifference3.entriesDiffering().entrySet()); // 输出: []
System.out.println(mapDifference3.entriesInCommon().entrySet()); // 输出: [a=a, b=1]
System.out.println(mapDifference3.entriesOnlyOnLeft().entrySet()); // 输出: [c=c]
System.out.println(mapDifference3.entriesOnlyOnRight().entrySet()); // 输出: [d=d]
查看文档 https://www.baeldung.com/guava-maps http://www.ibloger.net/article/3314.html http://www.ibloger.net/article/3294.html https://geek-docs.com/guava/guava-tutorials/multimap-in-java-guava.html http://t.zoukankan.com/kakaxisir-p-7248421.html https://zhuanlan.zhihu.com/p/359952176 https://www.cnblogs.com/hitechr/p/10481977.html https://blog.csdn.net/fenglailea/article/details/95324469 https://blog.csdn.net/neweastsun/article/details/79944839 http://ifeve.com/google-guava/