Google Guava v11 Collections示例

[b][color=red]利用apache Collections和google guava对list和map进行过滤和排序[/color][/b] [url]http://www.jiancool.com/article/50723356810/[/url]

官方[url]http://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6[/url]

基础工具--Guava-Libraries学习笔记
[url]http://www.cnblogs.com/htynkn/archive/2012/08/19/guava_1.html [/url]
Guava-Libraries是google对java的一个扩展,主要涵盖集合、缓存、并发、I/O、反射等等。
它本来是Google内部所使用的一个类库,后来整理开源出来了。这套库的设计引入了很多新的理念,研究一下可能会使你对Java这门语言有新的认识和看法。
地址:[url]http://code.google.com/p/guava-libraries/ [/url]
。。。
。。。

Google Guava集合4:创建方法和只读特性 [url]http://vipcowrie.iteye.com/blog/1522375[/url]


本文地址:[url]http://www.longtask.com/blog/?p=730[/url]
Guava 中文是石榴的意思,该项目是 Google 的一个开源项目,包含许多 Google 核心的 Java 常用库。目前主要包含:
[color=darkblue]com.google.common.annotations
com.google.common.base
com.google.common.cache
com.google.common.collect
com.google.common.eventbus
com.google.common.io
com.google.common.net
com.google.common.primitives
com.google.common.util.concurrent[/color]
这里先介绍一下最常用的com.google.common.collect包中的最常用的一些API,仅仅讨论一下API的使用方法,没有讨论到实现细节。

[b] 1:Collections的构造方法[/b]
我们平时直接创建Collections对象的方法一般都是用new关键字,有泛型的情况下看起来会比较长:
Map<String , Map<String , String>> see = new HashMap<String, Map<String,String>>();


在java7中,这个初始化做了简化:
Map<String , Map<String , String>> see = new HashMap<>();


可以通过Guava的API来这样写:
Map<String , Map<String , String>> see = Maps.newHashMap();


得到一个有size的Map:
Map<String , Map<String , String>> see = Maps.newHashMapWithExpectedSize(32);


在JDK的collection类型,在Guava中都可以找到相关的static的构造方法,例如:Lists , Sets , Maps , Queues。新的colleciont类型提供了直接构造的方法,例如:HashMultimap<String, String> multiMap = HashMultimap.create();

[b] 2:有限功能的函数式编程[/b]
介绍2个重要的接口:
[color=darkblue]com.google.common.base.Function : 根据输入值来得到输出值
com.google.common.base.Predicate : 根据输入值得到 true 或者 false[/color]

拿Collections2中有2个函数式编程的接口:filter , transform ,例如 :在Collection<Integer>中过滤大于某数的内容:

Collection<Integer> filterList = Collections2.filter(collections
, new Predicate<Integer>(){

@Override
public boolean apply(Integer input) {
if(input > 4)
return false;
else
return true;
}
});
list = new ArrayList<Integer>(filterList); //继续转到原来类型


把Lis<Integer>中的Integer类型转换为String , 并添加test作为后缀字符:
List<String> c2 = Lists.transform(list, new Function<Integer , String>(){

@Override

public String apply(Integer input) {

return String.valueOf(input) + "test";

}

});


需要说明的是每次调用返回都是新的对象,同时操作过程不是线程安全的。



[b] 3:Multimap and BiMap[/b]

Map中一个key只能有一个,后续put进去的内容会覆盖前面的内容,有些业务需要有相同的key,但是有不同的内容,Guava中提供了

Multimaps 来解决这个问题。

Multimap<String, String> prosons = HashMultimap.create();

prosons.put("longhao", "hubei");

prosons.put("lilei" , "henan");

prosons.put("longhao", "shanxi");

prosons.put("liuxia", "beijing");

prosons.put("lilei", "hainan");

Iterator<String> it = prosons.get("longhao").iterator();

while(it.hasNext()){

System.out.println(it.next());

}


BiMap可以有相同的key,但是不能有相同的value,如果不同的key设置了相同的value,则会抛出IllegalArgumentException异常,可以通过inverse()来反转kv,values()来获取value的set。
public void biMapShouldOnlyHaveUniqueValues() {

BiMap<Integer, String> biMap = HashBiMap.create();

biMap.put(1, "a");

biMap.put(2, "b");

biMap.put(3, "a"); //argh! an exception

}


[b] 4:tables[/b]

给出一个columns, rows , values, 这个API和Map<K , Map<K , V>>形式差不多,多了一些封装。例子:
static void tables(){

Table<Integer , String , Integer> user = HashBasedTable.create();

user.put(1, "longhao", 29);

user.put(1, "shuaige", 29);

user.put(2, "xiaomi", 1);

user.put(3, "soso", 3);

System.out.println(user.containsColumn("soso"));//true

System.out.println(user.containsColumn("3"));//false

System.out.println(user.contains(1, "xiaomi"));//false

System.out.println(user.contains(1, "meinv"));//true

System.out.println(user.row(1));//{shuaige=29, longhao=29}

}


[b] 5:更简洁的判断[/b]

使用Preconditions中的方法来判断是否为空等操作,这个操作和spring,apache common-lang中的API类似
import static com.google.common.base.Preconditions.checkArgument;

import static com.google.common.base.Preconditions.checkNotNull;

static void checkParam(String name , Integer passwd){

checkNotNull(name , passwd);

checkArgument("" != name , passwd > 0);

}


Common-lang,spring中的方法需要逐个调用。而Guava中支持。

[b] 6:约束[/b]

对Collection中的新加入元素做约束,只有符合条件的元素才能够被添加到Collection中,可以使用Constraint类来操作。

示例代码:
import static com.google.common.collect.Constraints.constrainedList;

static void constraintExam(){

Constraint<String> chkListStr = new Constraint<String>(){

@Override

public String checkElement(String element) {

if(element.startsWith("h")){

throw new IllegalArgumentException("不允许有h开头的内容");

}

return element;

}

};

List<String> list = Lists.newArrayList("li","hao","a");

List<String> conList = constrainedList(list, chkListStr);

conList.add("soso");

conList.add("hqb");// throw IllegalArgumentException

for(String str : list){

System.out.println(str);

}

}


参考资料

http://blog.solidcraft.eu/2010/10/googole-guava-v07-examples.html

http://insightfullogic.com/blog/2011/oct/21/5-reasons-use-guava/#

http://codingjunkie.net/google-guava-cache/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值