Guava 集合工具类 Maps
一、接口声明
以下是 com.google.common.collect.Maps 接口的声明:
@GwtCompatible(emulated=true) public final class Maps extends Object
二、接口方法
官方文档:https://google.github.io/guava/releases/27.0.1-jre/api/docs/com/google/common/collect/Maps.html
| 修饰符和类型 | 方法描述 |
|---|---|
static <A,B> Converter<A,B> | asConverter(BiMap<A,B> bimap) 返回一个Converter转换器,通过bimap.get()方法转换值,它的逆视图使用bimap.inverse().get()方法转换值. |
static <K,V> NavigableMap<K,V> | asMap(NavigableSet<K> set, Function<? super K,V> function) 返回NavigableSet集合的map表示,根据给定的Function从给定的Set中映射键值. |
static <K,V> Map<K,V> | asMap(Set<K> set, Function<? super K,V> function) 返回一个活动的map,键值为给定的set中的值,value为通过给定Function计算后的值. |
static <K,V> SortedMap<K,V> | asMap(SortedSet<K> set, Function<? super K,V> function) 返回有序Set集合的map表示,根据给定的Function从给定的Set中映射键值. |
static <K,V> MapDifference<K,V> | difference(Map<? extends K,? extends V> left, Map<? extends K,?extends V> right) 返回两个给定map之间的差异. |
static <K,V> MapDifference<K,V> | difference(Map<? extends K,? extends V> left, Map<? extends K,?extends V> right, Equivalence<? super V> valueEquivalence) 返回两个给定map之间的差异,通过所提供的valueEquivalence进行等值比较. |
static <K,V>SortedMapDifference<K,V> | difference(SortedMap<K,? extends V> left, Map<? extends K,? extendsV> right) 返回两个已排序的Map之间的差异,通过给定left的比较器. |
static <K,V> BiMap<K,V> | filterEntries(BiMap<K,V> unfiltered, Predicate<? superMap.Entry<K,V>> entryPredicate) 返回给定unfilteredMap.Entry中的Entry值通过给定entryPredicate过滤后的 BiMap映射. |
static <K,V> Map<K,V> | filterEntries(Map<K,V> unfiltered, Predicate<? super Map.Entry<K,V>> entryPredicate) 返回给定unfilteredMap.Entry中的Entry值通过给定entryPredicate过滤后的map 映射. |
static <K,V> NavigableMap<K,V> | filterEntries(NavigableMap<K,V> unfiltered, Predicate<? superMap.Entry<K,V>> entryPredicate) 返回给定unfilteredMap.Entry中的Entry值通过给定entryPredicate过滤后的 NavigableMap映射. |
static <K,V> SortedMap<K,V> | filterEntries(SortedMap<K,V> unfiltered, Predicate<? superMap.Entry<K,V>> entryPredicate) 返回给定unfilteredMap.Entry中的Entry值通过给定entryPredicate过滤后的 SortedMap映射. |
static <K,V> BiMap<K,V> | filterKeys(BiMap<K,V> unfiltered, Predicate<? super K> keyPredicate)返回给定unfilteredMap中的键值通过给定keyPredicate过滤后的BiMap映射. |
static <K,V> Map<K,V> | filterKeys(Map<K,V> unfiltered, Predicate<? super K> keyPredicate) 返回给定unfilteredMap中的键值通过给定keyPredicate过滤后的map映射. |
static <K,V> NavigableMap<K,V> | filterKeys(NavigableMap<K,V> unfiltered, Predicate<? super K>keyPredicate) 返回给定unfilteredMap中的键值通过给定keyPredicate过滤后的NavigableMap映射. |
static <K,V> SortedMap<K,V> | filterKeys(SortedMap<K,V> unfiltered, Predicate<? super K>keyPredicate) 返回给定unfilteredMap中的键值通过给定keyPredicate过滤后的SortedMap映射. |
static <K,V> BiMap<K,V> | filterValues(BiMap<K,V> unfiltered, Predicate<? super V>valuePredicate) 返回给定unfilteredMap中的value值通过给定keyPredicate过滤后的BiMap映射. |
static <K,V> Map<K,V> | filterValues(Map<K,V> unfiltered, Predicate<? super V>valuePredicate) 返回给定unfilteredMap中的value值通过给定keyPredicate过滤后的map映射. |
static <K,V> NavigableMap<K,V> | filterValues(NavigableMap<K,V> unfiltered, Predicate<? super V>valuePredicate) 返回给定unfilteredMap中的value值通过给定keyPredicate过滤后的 NavigableMap映射. |
static <K,V> SortedMap<K,V> | filterValues(SortedMap<K,V> unfiltered, Predicate<? super V>valuePredicate) 返回给定unfilteredMap中的value值通过给定keyPredicate过滤后的SortedMap映射. |
static ImmutableMap<String,String> | fromProperties(Properties properties) 通过给定的Properties实例,返回一个不可变的ImmutableMap实例. |
static <K,V> Map.Entry<K,V> | immutableEntry(K key, V value) 通过给定的key/value,返回一个不可变的map映射. |
static <K extends Enum<K>,V> ImmutableMap<K,V> | immutableEnumMap(Map<K,? extends V> map) 返回包含给定项的不可变映射实例。. |
static <K,V> ConcurrentMap<K,V> | newConcurrentMap() 返回一个通用的ConcurrentMap实例,支持ConcurrentMap接口的所有可选操作,它不允许null键或值,它是可序列化的. |
static <K extends Enum<K>,V> EnumMap<K,V> | newEnumMap(Class<K> type) 构造一个具有给定键类型的EnumMap实例. |
static <K extends Enum<K>,V> EnumMap<K,V> | newEnumMap(Map<K,? extends V> map) 构造一个与给定map有相同映射关系的EnumMap实例. |
static <K,V> HashMap<K,V> | newHashMap() :构造一个可变的空的HashMap实例. |
static <K,V> HashMap<K,V> | newHashMap(Map<? extends K,? extends V> map) 构造一个与给定map有相同映射关系的可变HashMap实例. |
static <K,V> HashMap<K,V> | newHashMapWithExpectedSize(int expectedSize) 构造一个期望长度为expectedSize的HashMap实例. |
static <K,V>IdentityHashMap<K,V> | newIdentityHashMap() 构造一个IdentityHashMap实例. |
static <K,V> LinkedHashMap<K,V> | newLinkedHashMap() 构造一个可变的、空的、插入排序的LinkedHashMap实例. |
static <K,V> LinkedHashMap<K,V> | newLinkedHashMap(Map<? extends K,? extends V> map) 构造一个可变的、插入排序的、与给定map有相同映射关系的LinkedHashMap实例. |
static <K,V> LinkedHashMap<K,V> | newLinkedHashMapWithExpectedSize(int expectedSize) 创建一个LinkedHashMap实例,具有足够高的“初始容量”,它应该保留expectedSize元素而不增长。 |
static <K extends Comparable,V>TreeMap<K,V> | newTreeMap() 构造一个可变的、空的TreeMap实例. |
static <C,K extends C,V>TreeMap<K,V> | newTreeMap(@Nullable Comparator<C> comparator) 构造一个可变的、空的、使用给定比较器的TreeMap实例. |
static <K,V> TreeMap<K,V> | newTreeMap(SortedMap<K,? extends V> map) 构造一个可变的、与给定SortedMap有相同映射关系的TreeMap实例. |
static <K extends Comparable<?super K>,V> NavigableMap<K,V> | subMap(NavigableMap<K,V> map, Range<K> range) 返回其键包含在范围内的map部分的视图。 |
static <K,V> BiMap<K,V> | synchronizedBiMap(BiMap<K,V> bimap) 返回一个同步的(线程安全)的bimap,由给定的bimap支持. |
static <K,V> NavigableMap<K,V> | synchronizedNavigableMap(NavigableMap<K,V> navigableMap) 返回一个同步的(线程安全)的NavigableMap,由给定的navigableMap支持. |
static <T,K extends Enum<K>,V> Collector<T,?,ImmutableMap<K,V>> | toImmutableEnumMap(Function<? super T,? extends K> keyFunction, Function<? super T,? extends V> valueFunction) 返回一个Collector,它将元素累积到ImmutableMap中,其中的键和值是将提供的映射函数应用于输入元素的结果. |
static <T,K extends Enum<K>,V> Collector<T,?,ImmutableMap<K,V>> | toImmutableEnumMap(Function<? super T,? extends K> keyFunction, Function<? super T,? extends V> valueFunction, BinaryOperator<V>mergeFunction) 返回一个Collector,它将元素累积到ImmutableMap中,其中的键和值是将提供的映射函数应用于输入元素的结果. |
static <K,V> ImmutableMap<K,V> | toMap(Iterable<K> keys, Function<? super K,V> valueFunction) 返回一个不可变的ImmutableMap实例,其键值为给定keys中去除重复值后的值,其值为键被计算了 valueFunction后的值. |
static <K,V> ImmutableMap<K,V> | toMap(Iterator<K> keys, Function<? super K,V> valueFunction) 返回一个不可变的ImmutableMap实例,其键值为给定keys中去除重复值后的值,其值为键被计算了 valueFunction后的值. |
static <K,V1,V2> Map<K,V2> | transformEntries(Map<K,V1> fromMap, Maps.EntryTransformer<? super K,?super V1,V2> transformer) 返回一个map映射,其Entry为给定fromMap.Entry通过给定EntryTransformer转换后的值. |
static <K,V1,V2>NavigableMap<K,V2> | transformEntries(NavigableMap<K,V1> fromMap, Maps.EntryTransformer<?super K,? super V1,V2> transformer) 返回一个NavigableMap映射,其Entry为给定fromMap.Entry通过给定 EntryTransformer转换后的值. |
static <K,V1,V2>SortedMap<K,V2> | transformEntries(SortedMap<K,V1> fromMap, Maps.EntryTransformer<?super K,? super V1,V2> transformer) 返回一个SortedMap映射,其Entry为给定fromMap.Entry通过给定EntryTransformer转 换后的值. |
static <K,V1,V2> Map<K,V2> | transformValues(Map<K,V1> fromMap, Function<? super V1,V2> function)返回一个map映射,其键值为给定fromMap的键值,其value为给定formMap中value通过Function转换后的值. |
static <K,V1,V2>NavigableMap<K,V2> | transformValues(NavigableMap<K,V1> fromMap, Function<? super V1,V2>function) 返回一个NavigableMap映射,其键值为给定fromMap的键值,其value为给定formMap中value通过 Function转换后的值. |
static <K,V1,V2>SortedMap<K,V2> | transformValues(SortedMap<K,V1> fromMap, Function<? super V1,V2>function) 返回一个SortedMap映射,其键值为给定fromMap的键值,其value为给定formMap中value通过 Function转换后的值. |
static <K,V> ImmutableMap<K,V> | uniqueIndex(Iterable<V> values, Function<? super V,K> keyFunction) 返回一个不可变的ImmutableMap实例,其值为按照给定顺序的给定的values值,键值为相应的值经过给定 Function计算后的值. |
static <K,V> ImmutableMap<K,V> | uniqueIndex(Iterator<V> values, Function<? super V,K> keyFunction) 返回一个不可变的ImmutableMap实例,其值为按照给定顺序的给定的values值,键值为相应的值经过给定 Function计算后的值. |
static <K,V> BiMap<K,V> | unmodifiableBiMap(BiMap<? extends K,? extends V> bimap) 返回给定的bimap的不可修改的BiMap表示. |
static <K,V> NavigableMap<K,V> | unmodifiableNavigableMap(NavigableMap<K,? extends V> map) 返回给定map的不可修改NavigableMap表示. |
三、测试类
package com.example.guava.collect;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.*;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.Test;
import java.util.*;
public class MapsTest {
/**
* newHashMap
* newHashMapWithExpectedSize
* newLinkedHashMap
* 4 种方式遍历
*/
@Test
public void test() {
Map<Integer, Integer> map0 = Maps.newHashMap();
for (int i = 0; i < 10; i++) {
map0.put(i, i);
}
System.out.println("map0:" + map0); // map0:{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}
Map<Integer, Integer> map1 = Maps.newHashMap(map0);
map1.put(10, 10);
System.out.println("map1:" + map1); // map1:{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10}
Map<Integer, Integer> map2 = Maps.newHashMapWithExpectedSize(3);
map2.put(1, 1);
map2.put(2, 2);
map2.put(3, 3);
System.out.println("map2:" + map2); // map2:{1=1, 2=2, 3=3}
Map<Integer, Integer> map3 = Maps.newLinkedHashMap(map1);
map3.put(11, 11);
System.out.println("map3:" + map3); // map3:{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, 11=11}
outMapKeyValue(map3);
}
/**
* 遍历Map的四种方法
*/
private static void outMapKeyValue(Map<Integer, Integer> map3) {
System.out.println("\n1.通过Map.entrySet遍历key和value");
for (Map.Entry<Integer, Integer> integerEntry : map3.entrySet()) {
System.out.println("key:" + integerEntry.getKey() + " value:" + integerEntry.getValue());
}
System.out.println("\n2.通过Map.entrySet使用iterator遍历key和value");
Iterator<Map.Entry<Integer, Integer>> it = map3.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, Integer> entry = it.next();
System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
}
System.out.println("\n3.通过Map.keySet遍历key;根据key得到value");
for (Integer integer : map3.keySet()) {
System.out.println("key:" + integer + " value:" + map3.get(integer));
}
System.out.println("\n4.通过Map.values()遍历所有的value,但不能遍历key");
for (Integer integer : map3.values()) {
System.out.println("value:" + integer);
}
}
/**
* difference:返回两个给定map之间的差异。
*/
@Test
public void difference() {
Map<String, String> map1 = new HashMap<String, String>() {
{
put("a", "1");
}
};
Map<String, String> map2 = new HashMap<String, String>() {
{
put("b", "2");
}
};
Map<String, String> map3 = new HashMap<String, String>() {
{
put("a", "3");
}
};
Map<String, String> map4 = new HashMap<String, String>() {
{
put("a", "1");
}
};
Map<String, String> map5 = ImmutableMap.of("a", "1");
System.out.println("\nMaps.difference(map1, map4)");
differencePrint(map1, map4);
System.out.println("\nMaps.difference(map1, map2)");
differencePrint(map1, map2);
System.out.println("\nMaps.difference(map1, map3)");
differencePrint(map1, map3);
System.out.println("\nMaps.difference(map1, map5)");
differencePrint(map1, map5);
}
private void differencePrint(Map<String, String> map1, Map<String, String> map4) {
MapDifference<String, String> difference = Maps.difference(map1, map4);
System.out.println(difference.areEqual());
System.out.println(difference.entriesInCommon());
System.out.println(difference.entriesOnlyOnRight());
System.out.println(difference.entriesOnlyOnLeft());
System.out.println(difference.entriesDiffering());
System.out.println(difference);
}
/**
* transformValues:返回一个map映射,其键值为给定fromMap的键值,其value为给定formMap中value通过Function转换后的值
* transformEntries:返回一个map映射, 其Entry为给定fromMap.Entry通过给定EntryTransformer转换后的值
*/
@Test
public void transformValues_transformEntries() {
Map<String, Boolean> fromMap = Maps.newHashMap();
fromMap.put("key", true);
fromMap.put("value", false);
// 对传入的元素取反
System.out.println(Maps.transformValues(fromMap, (Function<Boolean, Object>) input -> !input)); // 输出 {value=true, key=false}
// value为假,则key变大写
Maps.EntryTransformer<String, Boolean, String> entryTransformer = (key, value) -> value ? key : key.toUpperCase();
System.out.println(Maps.transformEntries(fromMap, entryTransformer)); // 输出 {value=VALUE, key=key}
}
/**
* filterKeys:返回给定unfilteredMap中的键值通过给定keyPredicate过滤后的map映射
* filterValues:返回给定unfilteredMap中的value值通过给定keyPredicate过滤后的map映射
* filterEntries:返回给定unfilteredMap.Entry中的Entry值通过给定entryPredicate过滤后的map映射
*/
@Test
public void filterKeys_filterValues_filterEntries() {
Map<String, Boolean> fromMap = ImmutableMap.of("abc", true, "bcd", false, "xyz", true);
Map<String, Boolean> keysMap = Maps.filterKeys(fromMap, new Predicate<String>() {
@Override
public boolean apply(@Nullable String input) {
// 过滤Map中键值包含字母y的元素
return input.contains("y");
}
});
System.out.println(keysMap); // {xyz=true}
// 过滤Map中key包含字母b的元素
System.out.println(Maps.filterKeys(fromMap, input -> input.contains("b"))); // {abc=true, bcd=false}
// 过滤Map中value值为假的元素
Map<String, Boolean> booleanMap = Maps.filterValues(fromMap, new Predicate<Boolean>() {
@Override
public boolean apply(@Nullable Boolean input) {
return !input;
}
});
System.out.println(booleanMap); // {bcd=false}
System.out.println(Maps.filterValues(fromMap, input -> input)); // {abc=true, xyz=true}
// 过滤Map.Entry中getValue()为真的元素
Map<String, Boolean> filterEntries = Maps.filterEntries(fromMap, new Predicate<Map.Entry<String, Boolean>>() {
@Override
public boolean apply(Map.@Nullable Entry<String, Boolean> input) {
return input.getValue();
}
});
System.out.println(filterEntries); // {abc=true, xyz=true}
}
/**
* asMap 视图
* toMap:返回一个不可变的ImmutableMap实例,其键值为给定keys中去除重复值后的值,其值为键被计算了valueFunction后的值
* uniqueIndex:返回一个不可变的ImmutableMap实例,其value值为按照给定顺序的给定的values值,键值为相应的值经过给定Function计算后的值
*/
@Test
public void testMaps() {
Set<String> set = Sets.newHashSet("a", "b", "c");
// Function:大写转换
Function<String, String> function = input -> input.toUpperCase();
System.out.println(Maps.asMap(set, function)); // {b=B, c=C, a=A}
List<String> keys = Lists.newArrayList("a", "b", "c", "a");
System.out.println(Maps.toMap(keys, function)); // {a=A, b=B, c=C}
List<String> values = Lists.newArrayList("a", "b", "c", "d");
System.out.println(Maps.uniqueIndex(values, function)); // {A=a, B=b, C=c, D=d}
}
}四、相关文章
未经允许请勿转载:程序喵 » Google Guava 快速入门 —— 【集合工具】Maps 类
程序喵