Guava简介及初体验
2015-10-31 19:31
615 查看
Guava是google提供的一个Java第三方库,旨在提供更简洁、优雅的api,让代码更加易读,易写、易用。
先看它的结构图:
而其中经常用到的如注解(annotations)、缓存(cache)、集合(collect)、哈希(hash)、io、数学运算(math)、net、原生类型(primitives)、反射(reflect)、并发(concurrent)。
下面将按照我的学习顺序依次介绍Guava.
不可变集合
运行结果:
1-2-3-4-
1-2-
1-2-3-4-
1-2-3-4-
新集合类型
[Google Guava] 2.2-新集合类型
集合工具类
这里用Lists举例
运行结果:
class java.lang.String1
class java.lang.String2
class java.lang.String3
class java.lang.String4
4
3
2
1
扩展工具类
让实现和扩展集合类变得更容易,想要了解更多信息,可以转到[Google Guava] 2.4-集合扩展工具类。
先看它的结构图:
而其中经常用到的如注解(annotations)、缓存(cache)、集合(collect)、哈希(hash)、io、数学运算(math)、net、原生类型(primitives)、反射(reflect)、并发(concurrent)。
下面将按照我的学习顺序依次介绍Guava.
Collect
Guava为我们提供了许多Java之外的集合以及集合工具类,主要分为四大类:不可变集合、新集合类型、集合工具类、扩展工具类。不可变集合
ImmutableList<Integer> immutableList = ImmutableList.of(1, 2, 3, 4); ImmutableBiMap<Integer, String> immutableBiMap = ImmutableBiMap.of(1, "1", 2, "2"); ImmutableSet<Integer> immutableSet = ImmutableSet.of(1, 2, 3, 4); ImmutableCollection<Integer> immutableCollection = ImmutableList.of(1, 2, 3, 4); for (Integer i : immutableList) { System.out.print(i + "-"); } System.out.println(); for (Integer i : immutableBiMap.keySet()) { System.out.print(immutableBiMap.get(i) + "-"); } System.out.println(); for (Integer i : immutableList) { System.out.print(i + "-"); } System.out.println(); for (Integer i : immutableCollection) { System.out.print(i + "-"); }
运行结果:
1-2-3-4-
1-2-
1-2-3-4-
1-2-3-4-
新集合类型
[Google Guava] 2.2-新集合类型
集合工具类
这里用Lists举例
ArrayList<Integer> list = Lists.newArrayList(new Integer[]{1,2,3,4}); List<String> transform = Lists.transform(list, new Function<Integer, String>() { @Override public String apply(Integer integer) { return integer + ""; } }); List<Integer> reverseList = Lists.reverse(list); for (Object o : transform) { System.out.println(o.getClass().toString() + o); } for (Object o : reverseList) { System.out.println(o); }
运行结果:
class java.lang.String1
class java.lang.String2
class java.lang.String3
class java.lang.String4
4
3
2
1
扩展工具类
让实现和扩展集合类变得更容易,想要了解更多信息,可以转到[Google Guava] 2.4-集合扩展工具类。
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统