链接地址:http://www.xx566.com/detail/140.html
前面我们依次学习了Guava Collections中Lists、Sets这两个类,分别用于处理List和Set这两种数据结构,在日常工作开发中,除了这两种数据结构,我们经常还需要用到Map,一种键值对存储的数据结构,Guava同样为我们提供了Maps类,用于处理Map实例的实用类,接下来,我们就来整理学习一下Maps类。
翻开Maps的源代码,我们简单来梳理一下里面的静态公共方法,如下:
immutableEnumMap( Map<K, ? extends V> map):返回包含给定项的不可变映射实例。
newHashMap():构造一个可变的空的HashMap实例。
newHashMapWithExpectedSize(int expectedSize):构造一个期望长度为expectedSize的HashMap实例。
newHashMap( Map<? extends K, ? extends V> map):构造一个与给定map有相同映射关系的可变HashMap实例。
newLinkedHashMap():构造一个可变的、空的、插入排序的LinkedHashMap实例。
newLinkedHashMap( Map<? extends K, ? extends V> map):构造一个可变的、插入排序的、与给定map有相同映射关系的LinkedHashMap实例。
newConcurrentMap():返回一个通用的ConcurrentMap实例,支持ConcurrentMap接口的所有可选操作,它不允许null键或值,它是可序列化的。
newTreeMap():构造一个可变的、空的TreeMap实例。
newTreeMap(SortedMap<K, ? extends V> map):构造一个可变的、与给定SortedMap有相同映射关系的TreeMap实例。
newTreeMap( @Nullable Comparator<C> comparator):构造一个可变的、空的、使用给定比较器的TreeMap实例。
newEnumMap(Class<K> type):构造一个具有给定键类型的EnumMap实例。
newEnumMap( Map<K, ? extends V> map):构造一个与给定map有相同映射关系的EnumMap实例。
newIdentityHashMap():构造一个IdentityHashMap实例。
difference( Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right):返回两个给定map之间的差异。
difference( Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right, Equivalence<? super V> valueEquivalence):返回两个给定map之间的差异,通过所提供的valueEquivalence进行等值比较。
difference( SortedMap<K, ? extends V> left, Map<? extends K, ? extends V> right):返回两个已排序的Map之间的差异,通过给定left的比较器。
asMap( Set<K> set, Function<? super K, V> function):返回一个活动的map,键值为给定的set中的值,value为通过给定Function计算后的值。
asMap( SortedSet<K> set, Function<? super K, V> function):返回有序Set集合的map表示,根据给定的Function从给定的Set中映射键值。
asMap( NavigableSet<K> set, Function<? super K, V> function):返回NavigableSet集合的map表示,根据给定的Function从给定的Set中映射键值。
toMap(Iterable<K> keys,Function<? super K, V> valueFunction)、toMap(Iterator<K> keys,Function<? super K, V> valueFunction):返回一个不可变的ImmutableMap实例,其键值为给定keys中去除重复值后的值,其值为键被计算了 valueFunction后的值。
uniqueIndex(Iterable<V> values, Function<? super V, K> keyFunction)、uniqueIndex(Iterator<V> values, Function<? super V, K> keyFunction):返回一个不可变的ImmutableMap实例,其值为按照给定顺序的给定的values值,键值为相应的值经过给定 Function计算后的值。
fromProperties(Properties properties):通过给定的Properties实例,返回一个不可变的ImmutableMap实例。
immutableEntry(@Nullable K key, @Nullable V value):通过给定的key/value,返回一个不可变的map映射。
asConverter(final BiMap<A, B> bimap):返回一个Converter转换器,通过bimap.get()方法转换值,它的逆视图使用bimap.inverse().get()方法转换值。
synchronizedBiMap(BiMap<K, V> bimap):返回一个同步的(线程安全)的bimap,由给定的bimap支持。
unmodifiableBiMap( BiMap<? extends K, ? extends V> bimap):返回给定的bimap的不可修改的BiMap表示。
transformValues( Map<K, V1> fromMap, Function<? super V1, V2> function):返回一个map映射,其键值为给定fromMap的键值,其value为给定formMap中value通过Function转换后 的值。
transformValues( SortedMap<K, V1> fromMap, Function<? super V1, V2> function):返回一个SortedMap映射,其键值为给定fromMap的键值,其value为给定formMap中value通过 Function转换后的值。
transformValues( NavigableMap<K, V1> fromMap, Function<? super V1, V2> function):返回一个NavigableMap映射,其键值为给定fromMap的键值,其value为给定formMap中value通过 Function转换后的值。
transformEntries( Map<K, V1> fromMap, EntryTransformer<? super K, ? super V1, V2> transformer):返回一个map映射,其Entry为给定fromMap.Entry通过给定EntryTransformer转换后的值。
transformEntries(SortedMap<K, V1> fromMap, EntryTransformer<? super K, ? super V1, V2> transformer):返回一个SortedMap映射,其Entry为给定fromMap.Entry通过给定EntryTransformer转 换后的值。
transformEntries( final NavigableMap<K, V1> fromMap,EntryTransformer<? super K, ? super V1, V2> transformer):返回一个NavigableMap映射,其Entry为给定fromMap.Entry通过给定 EntryTransformer转换后的值。
filterKeys(Map<K, V> unfiltered, final Predicate<? super K> keyPredicate):返回给定unfilteredMap中的键值通过给定keyPredicate过滤后的map映射。
filterKeys(SortedMap<K, V> unfiltered, final Predicate<? super K> keyPredicate):返回给定unfilteredMap中的键值通过给定keyPredicate过滤后的SortedMap映射。
filterKeys(NavigableMap<K, V> unfiltered, final Predicate<? super K> keyPredicate):返回给定unfilteredMap中的键值通过给定keyPredicate过滤后的NavigableMap映射。
filterKeys(BiMap<K, V> unfiltered, final Predicate<? super K> keyPredicate):返回给定unfilteredMap中的键值通过给定keyPredicate过滤后的BiMap映射。
filterValues( Map<K, V> unfiltered, final Predicate<? super V> valuePredicate):返回给定unfilteredMap中的value值通过给定keyPredicate过滤后的map映射。
filterValues(SortedMap<K, V> unfiltered, final Predicate<? super V> valuePredicate):返回给定unfilteredMap中的value值通过给定keyPredicate过滤后的SortedMap映 射。
filterValues(NavigableMap<K, V> unfiltered, final Predicate<? super V> valuePredicate):返回给定unfilteredMap中的value值通过给定keyPredicate过滤后的 NavigableMap映射。
filterValues(BiMap<K, V> unfiltered, final Predicate<? super V> valuePredicate):返回给定unfilteredMap中的value值通过给定keyPredicate过滤后的BiMap映射。
filterEntries( Map<K, V> unfiltered, Predicate<? super Entry<K, V>> entryPredicate):返回给定unfilteredMap.Entry中的Entry值通过给定entryPredicate过滤后的map 映射。
filterEntries( SortedMap<K, V> unfiltered, Predicate<? super Entry<K, V>> entryPredicate):返回给定unfilteredMap.Entry中的Entry值通过给定entryPredicate过滤后的 SortedMap映射。
filterEntries( NavigableMap<K, V> unfiltered, Predicate<? super Entry<K, V>> entryPredicate):返回给定unfilteredMap.Entry中的Entry值通过给定entryPredicate过滤后的 NavigableMap映射。
filterEntries( BiMap<K, V> unfiltered, Predicate<? super Entry<K, V>> entryPredicate):返回给定unfilteredMap.Entry中的Entry值通过给定entryPredicate过滤后的 BiMap映射。
unmodifiableNavigableMap(NavigableMap<K, V> map):返回给定map的不可修改NavigableMap表示。
synchronizedNavigableMap( NavigableMap<K, V> navigableMap):返回一个同步的(线程安全)的NavigableMap,由给定的navigableMap支持。
Maps类中提供的方法很多,对其中的一些方法也是一知半解,需要慢慢摸索,这里对其中的一些重点和特别的方法做一些示例学习,其他的方法不再赘述,示例代码如下:
/**
* Maps:处理Map实例的实用类
* Maps里面方法很多,一知半解,慢慢摸索
* User: Realfighter
* Date: 2014/9/5
* Time: 15:11
*/
public class MapsTest {
@Test
public void testMaps() {
/**
* difference:返回两个给定map之间的差异。
*/
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");
}
};
//输出:not equal: only on left={a=1}: only on right={b=2}
System.out.println(Maps.difference(map1, map2));
//输出:not equal: value differences={a=(1, 3)}
System.out.println(Maps.difference(map1, map3));
/**
* asMap:返回一个活动的map
* 键值为给定的set中的值
* value为通过给定Function计算后的值。
*/
Set<String> set = Sets.newHashSet("a", "b", "c");
//Function:简单的对元素做大写转换,下面示例多次使用
Function<String, String> function = new Function<String, String>() {
@Override
public String apply(String input) {
return input.toUpperCase();
}
};
//输出:{b=B, c=C, a=A}
System.out.println(Maps.asMap(set, function));
/**
* toMap:返回一个不可变的ImmutableMap实例
* 其键值为给定keys中去除重复值后的值
* 其值为键被计算了valueFunction后的值
*/
List<String> keys = Lists.newArrayList("a", "b", "c", "a");
//输出:{a=A, b=B, c=C}
System.out.println(Maps.toMap(keys, function));
/**
* uniqueIndex:返回一个不可变的ImmutableMap实例,
* 其value值为按照给定顺序的给定的values值
* 键值为相应的值经过给定Function计算后的值
*/
List<String> values = Lists.newArrayList("a", "b", "c", "d");
/**
* 注:这里的value值不可重复,重复的话在转换后会抛出异常:
* IllegalArgumentException: Multiple entries with same key
*/
//输出:{A=a, B=b, C=c, D=d}
System.out.println(Maps.uniqueIndex(values, function));
/**
* transformValues:返回一个map映射
* 其键值为给定fromMap的键值
* 其value为给定formMap中value通过Function转换后的值
*/
Map<String, Boolean> fromMap = Maps.newHashMap();
fromMap.put("key", true);
fromMap.put("value", false);
//输出:{value=true, key=false}
System.out.println(Maps.transformValues(fromMap, new Function<Boolean, Object>() {
@Override
public Object apply(Boolean input) {
//对传入的元素取反
return !input;
}
}));
/**
* transformEntries:返回一个map映射
* 其Entry为给定fromMap.Entry通过给定EntryTransformer转换后的值
*/
Maps.EntryTransformer<String, Boolean, String> entryTransformer = new Maps.EntryTransformer<String, Boolean, String>() {
public String transformEntry(String key, Boolean value) {
//value为假,则key变大写
return value ? key : key.toUpperCase();
}
};
//输出:{value=VALUE, key=key}
System.out.println(Maps.transformEntries(fromMap, entryTransformer));
/**
* filterKeys:返回给定unfilteredMap中的键值通过给定keyPredicate过滤后的map映射
*/
//输出:{key=true}
System.out.println(Maps.filterKeys(fromMap, new Predicate<String>() {
@Override
public boolean apply(String input) {
//过滤Map中键值包含字母y的元素
return input.contains("y");
}
}));
/**
* filterValues:返回给定unfilteredMap中的value值通过给定keyPredicate过滤后的map映射
*/
//输出:{value=false}
System.out.println(Maps.filterValues(fromMap, new Predicate<Boolean>() {
@Override
public boolean apply(Boolean input) {
//过滤Map中value值为假的元素
return !input;
}
}));
/**
* filterEntries:返回给定unfilteredMap.Entry中的Entry值通过给定entryPredicate过滤后的map映射
*/
//输出:{key=true}
System.out.println(Maps.filterEntries(fromMap, new Predicate<Map.Entry<String, Boolean>>() {
@Override
public boolean apply(Map.Entry<String, Boolean> input) {
//过滤Map.Entry中getValue()为真的元素
return input.getValue();
}
}));
}
}