Maps包方法列表:

还是泛型创建Map:

public static <K, V> HashMap<K, V> newHashMap() {
return new HashMap<K, V>();
} public static <K, V> HashMap<K, V> newHashMapWithExpectedSize(int expectedSize) {
return new HashMap<K, V>(capacity(expectedSize));
} public static <K, V> HashMap<K, V> newHashMap(Map<? extends K, ? extends V> map) {
return new HashMap<K, V>(map);
} public static <K, V> LinkedHashMap<K, V> newLinkedHashMap() {
return new LinkedHashMap<K, V>();
} public static <K, V> LinkedHashMap<K, V> newLinkedHashMapWithExpectedSize(int expectedSize) {
return new LinkedHashMap<K, V>(capacity(expectedSize));
} public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(Map<? extends K, ? extends V> map) {
return new LinkedHashMap<K, V>(map);
} public static <K, V> ConcurrentMap<K, V> newConcurrentMap() {
return new MapMaker().<K, V>makeMap();
}
public static <K extends Comparable, V> TreeMap<K, V> newTreeMap() {
return new TreeMap<K, V>();
}

还有一些EnumMap、IdentitiyMap等,但是不经常用,就不贴了。

Map中的不同,还有其他几种方式,这里就只贴个最常用的吧。

 public static <K, V> MapDifference<K, V> difference(
Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) {
if (left instanceof SortedMap) {
SortedMap<K, ? extends V> sortedLeft = (SortedMap<K, ? extends V>) left;
SortedMapDifference<K, V> result = difference(sortedLeft, right);
return result;
}
return difference(left, right, Equivalence.equals());
}

Set和其他内容转Map,这个方法在获得某些对象转Map操作比较好用。

  public static <K, V> Map<K, V> asMap(Set<K> set, Function<? super K, V> function) {
if (set instanceof SortedSet) {
return asMap((SortedSet<K>) set, function);
} else {
return new AsMapView<K, V>(set, function);
}
}

某些对象转不可变Map,和上面一样,你只要声明一个Guava的Function覆盖以下它的方法然后传入进来即可:

 public static <K, V> ImmutableMap<K, V> toMap(
Iterable<K> keys, Function<? super K, V> valueFunction) {
return toMap(keys.iterator(), valueFunction);
}

接下来这个有点牛逼的,然后从来不知道该用在哪里的方法,反正我是没碰到这种场景:

  /**
* Returns a view of a map where each value is transformed by a function. All
* other properties of the map, such as iteration order, are left intact. For
* example, the code: <pre> {@code
*
* Map<String, Integer> map = ImmutableMap.of("a", 4, "b", 9);
* Function<Integer, Double> sqrt =
* new Function<Integer, Double>() {
* public Double apply(Integer in) {
* return Math.sqrt((int) in);
* }
* };
* Map<String, Double> transformed = Maps.transformValues(map, sqrt);
* System.out.println(transformed);}</pre>
*
* ... prints {@code {a=2.0, b=3.0}}.
*
public static <K, V1, V2> Map<K, V2> transformValues(
Map<K, V1> fromMap, Function<? super V1, V2> function) {
return transformEntries(fromMap, asEntryTransformer(function));
}

基本上每种类型的Map都会有几个方法去处理,不一一列举。

接下里又是传入Prediction过滤Map:

  @CheckReturnValue
public static <K, V> Map<K, V> filterKeys(
Map<K, V> unfiltered, final Predicate<? super K> keyPredicate) {
if (unfiltered instanceof SortedMap) {
return filterKeys((SortedMap<K, V>) unfiltered, keyPredicate);
} else if (unfiltered instanceof BiMap) {
return filterKeys((BiMap<K, V>) unfiltered, keyPredicate);
}
checkNotNull(keyPredicate);
Predicate<Entry<K, ?>> entryPredicate = keyPredicateOnEntries(keyPredicate);
return (unfiltered instanceof AbstractFilteredMap)
? filterFiltered((AbstractFilteredMap<K, V>) unfiltered, entryPredicate)
: new FilteredKeyMap<K, V>(checkNotNull(unfiltered), keyPredicate, entryPredicate);
}
  @CheckReturnValue
public static <K, V> Map<K, V> filterValues(
Map<K, V> unfiltered, final Predicate<? super V> valuePredicate) {
if (unfiltered instanceof SortedMap) {
return filterValues((SortedMap<K, V>) unfiltered, valuePredicate);
} else if (unfiltered instanceof BiMap) {
return filterValues((BiMap<K, V>) unfiltered, valuePredicate);
}
return filterEntries(unfiltered, Maps.<V>valuePredicateOnEntries(valuePredicate));
}
  @CheckReturnValue
public static <K, V> Map<K, V> filterEntries(
Map<K, V> unfiltered, Predicate<? super Entry<K, V>> entryPredicate) {
if (unfiltered instanceof SortedMap) {
return filterEntries((SortedMap<K, V>) unfiltered, entryPredicate);
} else if (unfiltered instanceof BiMap) {
return filterEntries((BiMap<K, V>) unfiltered, entryPredicate);
}
checkNotNull(entryPredicate);
return (unfiltered instanceof AbstractFilteredMap)
? filterFiltered((AbstractFilteredMap<K, V>) unfiltered, entryPredicate)
: new FilteredEntryMap<K, V>(checkNotNull(unfiltered), entryPredicate);
}

应该我碰到的场景太少了,List、set、map中都有大量代码去处理Navigate类型和immutable类型的集合,感觉还是用的太少了。需要再深入研究一下。

Guava包学习---Maps的更多相关文章

  1. Guava包学习---Lists

    Guava包是我最近项目中同事推荐使用的,是google推出的库.里面的功能非常多,包括了集合.缓存.原生类型支持.并发库.通用注解.字符串处理.IO等.我们项目中使用到了guava依赖,但是实际上只 ...

  2. Guava包学习--EventBus

    之前没用过这个EventBus,然后看了一下EventBus的源码也没看明白,(-__-)b.反正大概就是弄一个优雅的方式实现了观察者模式吧.慢慢深入学习一下. 观察者模式其实就是生产者消费者的一个变 ...

  3. Guava包学习-Cache

    这段时间用到了ehcache和memcache,memcache只用来配置在tomcat中做负载均衡过程中的session共享,然后ehcache用来存放需要的程序中缓存. Guava中的Cache和 ...

  4. Guava包学习---Bimap

    Bimap也是Guava中提供的新集合类,别名叫做双向map,就是key->value,value->key,也就是你可以通过key定位value,也可以用value定位key. 这个场景 ...

  5. Guava包学习---Sets

    Sets包的内容和上一篇中的Lists没有什么大的区别,里面有些细节可以看一下: 开始的创建newHashSet()的各个重载方法.newConcurrentHashSet()的重载方法.newTre ...

  6. Guava包学习--Hash

    我们HashMap会有一个rehash的过程,为什么呢?因为java内建的散列码被限制为32位,而且没有分离散列算法和所作用的数据,所以替代算法比较难做.我们使用HashMap的时候它自身有一个reh ...

  7. Guava包学习---I/O

    Guava的I/O平时使用不太多,目前项目原因导致基本上只有在自己写一些文本处理小工具才用得到.但是I/O始终是程序猿最常遇到的需求和面试必问的知识点之一.同时Guava的I/O主要面向是时JDK5和 ...

  8. Guava包学习--Table

    Table,顾名思义,就好像HTML中的Table元素一样,其实就是行+列去确定的值,更准确的比喻其实就是一个二维矩阵. 其实它就是通过行+列两个key去找到一个value,然后它又containsv ...

  9. Guava包学习-Multimap

    它和上一章的MultiSet的继承结果很相似,只不过在上层的接口是Multimap不是Multiset. Multimap的特点其实就是可以包含有几个重复Key的value,你可以put进入多个不同v ...

随机推荐

  1. jquery实现的让图片在网页的可视区域里四处漂浮的效果

    本文分享给大家一个用jquery开发的图片漂浮效果. jquery图片漂浮效果,常见的就是网页里四处漂来漂去的广告了,漂到边缘时还会反弹和拐弯. 下面来看具体的代码,先看要实现漂亮效果的这个jquer ...

  2. JS 监听键盘按键

    1. 实现Ctrl+ Enter 组合键触发事件 document.onkeydown=function(event){ var keyNum = window.event ? event.keyCo ...

  3. HTTP的response code 1xx,2xx,3xx,4xx,5xx分别代表什么

    HTTP 状态码点击链接可了解详情.您也可以访问 HTTP 状态码上的 W3C 页获取更多信息. 1xx(临时响应):表示临时响应并需要请求者继续执行操作的状态码. 2xx (成功):表示成功处理了请 ...

  4. Bootstrap 3.0的扁平化来了

    Bootstrap 3 RC1 发布了,从官方上看,Bootstrap 3 似乎也开始趋于扁平化的风格设计. 网站UI和Button bootstrap 2.3.2以下的整体UI和图标是以box-sh ...

  5. 【数据库】10.0 MySQL常用语句(一)

    显示数据库语句: SHOW DATABASES    只是显示数据库的名字 显示数据库创建语句: SHOW CREATE DATABASE db_name 数据库删除语句: DROP DATABASE ...

  6. 04.CSS动画示例-->烟花

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. vscode 实用的插件

    REST-Client api接口测试插件 在项目中新建以.http后缀名的文件即可. 右键使用 可以生产多种语言的请求代码块.可以说是非常舒服了. 使用代码块功能生成了node环境的http请求. ...

  8. MySQL数据库(10)----IN 和 NOT IN 子查询

    当子查询要返回多个行来与外层查询进行比较运算时,可以使用运算符 IN 和 NOT IN.它们会测试某个给定的比较值是否存在于某一组值里.如果外层查询里的行与子查询返回的某一个行相匹配,那么 IN 的结 ...

  9. Box(视图组件)如何在多个页面不同视觉规范下的复用

    本文来自 网易云社区 . 问题描述 Android App中的页面元素,都是由一个个Box(可以理解成一个个自定义View组件和Widget同级)组成,这些Box可以在不同的页面.不同的模块达到复用的 ...

  10. EventTarge Node Docuement Element HTMLElement 关系

    综述: 可以将其看做是依次继承的关系: Node Node A Node is an interface from which a number of DOM types inherit, and a ...