Google的Guava它Collection升华
至于Guava这是不是在这里说。一个已被提上一个非常特殊的!
这主要是为了分享Guava对于一些升华处理组。井,不多说了,直接在代码:
package com.joyce.guava.bean; /**
* 学生实体类
*
* @author Joyce.Luo
* @date 2014-6-19 下午02:37:19
*/
public class Student {
/**
* 学号
*/
private Integer stuId;
/**
* 姓名
*/
private String stuName;
/**
* 年龄
*/
private Integer stuAge; /**
* @return the stuId
*/
public Integer getStuId() {
return stuId;
} /**
* @param stuId
* the stuId to set
*/
public void setStuId(Integer stuId) {
this.stuId = stuId;
} /**
* @return the stuName
*/
public String getStuName() {
return stuName;
} /**
* @param stuName
* the stuName to set
*/
public void setStuName(String stuName) {
this.stuName = stuName;
} /**
* @return the stuAge
*/
public Integer getStuAge() {
return stuAge;
} /**
* @param stuAge
* the stuAge to set
*/
public void setStuAge(Integer stuAge) {
this.stuAge = stuAge;
} /**
*
*/
public Student() {
super();
} /**
* @param stuId
* @param stuName
* @param stuAge
*/
public Student(Integer stuId, String stuName, Integer stuAge) {
super();
this.stuId = stuId;
this.stuName = stuName;
this.stuAge = stuAge;
}
}
实体类有了。主要是为了在集合中使用提供方便。关键在于:
/**
* @Description:
*
* @Title: SetGuava.java
* @Package com.joyce.guava.main
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 上午11:03:53
* @version V2.0
*/
package com.joyce.guava.main; import java.util.Collections;
import java.util.List;
import java.util.Map; import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.BiMap;
import com.google.common.collect.ClassToInstanceMap;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import com.google.common.collect.MutableClassToInstanceMap;
import com.google.common.collect.Ordering;
import com.google.common.collect.Table;
import com.joyce.guava.bean.Student; /**
* @Description: Guava的集合
*
* @ClassName: SetGuava
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 上午11:03:53
* @version V2.0
*/
public class SetGuava {
public static void main(String[] args) {
/**
* Guava API 提供了实用的新的集合类型, 协同已经存在的java集合工作的非常好。 各自是 Multimap, Multiset,
* Table, BiMap。 ClassToInstanceMap
*/
System.out.println("Multimap:一种key能够反复的map,子类有ListMultimap和SetMultimap,相应的通过key分别得到list和set");
testMultimap();
System.out.println("Multiset:不是集合,能够添加反复的元素,而且能够统计出反复元素的个数");
testMulitiset();
System.out.println("Table:相当于有两个key的map");
testTable();
System.out.println("BiMap: 是一个一一映射,能够通过key得到value,也能够通过value得到key");
testBitMap();
System.out.println("ClassToInstanceMap:map的key并非仅仅是一种类型");
testClassToInstanceMap();
System.out.println("排序,是guava一份非常灵活的比較类,能够被用来操作。扩展,当作比較器,排序提供了集合排序的非常多控制 ");
testOrder();
} /**
* @Description: Multimap:一种key能够反复的map。子类有ListMultimap和SetMultimap。相应的通过key分别得到list和set
*
*
* @Title: SetGuava.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 上午11:19:50
* @version V2.0
*/
private static void testMultimap() {
Multimap<String, Student> customersByType = ArrayListMultimap.create();
customersByType.put("abc", new Student(1, "Joyce", 20));
customersByType.put("abc", new Student(1, "Joyce One", 20));
customersByType.put("abc", new Student(1, "Joyce Two", 20));
customersByType.put("abc", new Student(1, "Joyce Three", 20));
customersByType.put("abcd", new Student(1, "Joyce Four", 20));
customersByType.put("abcde", new Student(1, "Joyce Five", 20));
System.out.println(customersByType.get("abc").size());
for (Student stu : customersByType.get("abc")) {
System.out.println(stu.getStuName());
}
} /**
* @Description: Multiset:不是集合。能够添加反复的元素。而且能够统计出反复元素的个数
*
*
* @Title: SetGuava.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 上午11:19:59
* @version V2.0
*/
private static void testMulitiset() {
Multiset<Integer> multiSet = HashMultiset.create();
multiSet.add(10);
multiSet.add(30);
multiSet.add(30);
multiSet.add(40);
System.out.println(multiSet.count(30)); // 2 -- 统计XX出现的次数
System.out.println(multiSet.size()); // 4 -- 元素的个数
} /**
* @Description: Table:相当于有两个key的map
*
*
* @Title: SetGuava.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 上午11:24:43
* @version V2.0
*/
private static void testTable() {
Table<Integer, Integer, Student> personTable = HashBasedTable.create();
personTable.put(1, 20, new Student(1, "46546", 20));
personTable.put(0, 30, new Student(2, "46546", 30));
personTable.put(0, 25, new Student(3, "46546", 25));
personTable.put(1, 50, new Student(4, "46546", 50));
personTable.put(0, 27, new Student(5, "46546", 27));
personTable.put(1, 29, new Student(6, "46546", 29));
personTable.put(0, 38, new Student(7, "46546", 38));
personTable.put(1, 66, new Student(8, "46546", 66)); // 得到行集合
Map<Integer, Student> rowMap = personTable.row(0);
Integer rowMax = Collections.max(rowMap.keySet());
System.out.println(rowMax);
} /**
* @Description: BiMap: 是一个一一映射。能够通过key得到value。也能够通过value得到key
*
*
* @Title: SetGuava.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 上午11:36:59
* @version V2.0
*/
private static void testBitMap() {
// 双向map
BiMap<Integer, String> biMap = HashBiMap.create();
biMap.put(1, "hello");
biMap.put(2, "helloa");
biMap.put(3, "world");
biMap.put(4, "worldb");
biMap.put(5, "my");
biMap.put(6, "myc");
// 通过key取value
String value = biMap.get(5);
System.out.println("key -- [5] ; value -- [" + value + "]");
// 通过value取key
Integer key = biMap.inverse().get("my");
System.out.println("value -- [my] ; key -- [" + key + "]");
} /**
* @Description: ClassToInstanceMap:有的时候,你的map的key并非一种类型,他们是非常多类型,你想通过映射他们得到这样的类型,
* guava提供了ClassToInstanceMap满足了这个目的。
*
* 除了继承自Map接口,ClassToInstaceMap提供了方法 T getInstance(Class<T>) 和
* T putInstance(Class<T>, T),消除了强制类型转换。 *
* 该类有一个简单类型的參数。通常称为B,代表了map控制的上层绑定,比如:
* ClassToInstanceMap<Number> numberDefaults =
* MutableClassToInstanceMap.create();
* numberDefaults.putInstance(Integer.class,
* Integer.valueOf(0));
*
* 从技术上来说,ClassToInstanceMap<B> 实现了Map<Class<? extends B>,
* B>。或者说,这是一个从B的子类到B对象的映射,这可能使得ClassToInstanceMap的泛型轻度混乱。
* 可是仅仅要记住B总是Map的上层绑定类型,通常来说B仅仅是一个对象。 guava提供了实用的实现。
* MutableClassToInstanceMap 和 ImmutableClassToInstanceMap.
* 重点:像其它的Map<Class,Object>,ClassToInstanceMap
* 含有的原生类型的项目,一个原生类型和他的相应的包装类能够映射到不同的值;
*
*
*
* @Title: SetGuava.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 上午11:42:52
* @version V2.0
*/
private static void testClassToInstanceMap() {
ClassToInstanceMap<Student> classToInstanceMap = MutableClassToInstanceMap.create();
Student stu = new Student(1, "Joyce", 20);
classToInstanceMap.putInstance(Student.class, stu);
Student stuObj = classToInstanceMap.getInstance(Student.class);
System.out.println(stuObj.getStuName());
} /**
* @Description:排序,是guava一份非常灵活的比較类。能够被用来操作。扩展,当作比較器,排序提供了集合排序的非常多控制
*
*
* @Title: SetGuava.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 上午11:49:30
* @version V2.0
*/
private static void testOrder(){
List<Integer> numberList = Lists.newArrayList(30, 20, 60, 80, 10);
System.out.println(Ordering.natural().sortedCopy(numberList)); //10,20,30,60,80
System.out.println(Ordering.natural().reverse().sortedCopy(numberList)); //80,60,30,20,10
System.out.println(Ordering.natural().min(numberList));//10
System.out.println(Ordering.natural().max(numberList));//80
numberList = Lists.newArrayList(30, 20, 60, 80, null, 10);
System.out.println(Ordering.natural().nullsLast().sortedCopy(numberList));//10, 20,30,60,80,null
System.out.println(Ordering.natural().nullsFirst().sortedCopy(numberList));//null,10,20,30,60,80
}
}
效果如图:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHVvMjAxMjI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
相信大家伙对上面的代码假设理解深入了的话。会明确了的!。!
事实上Guava在集合上面还提供了其他方法,这里就不一一分享了,有兴趣的大伙儿能够自己是私底下去好好研究一下!Guava资源下载地址:http://download.csdn.net/detail/luo201227/7207227,附上本人demo资源下载地址:http://download.csdn.net/download/luo201227/7581845。!!
Ok。今天就到这里!
下次有机会交谈,与大家分享Guava其他升华。。
。
Google的Guava它Collection升华的更多相关文章
- Google的Guava之IO升华
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/luo201227/article/details/36413279 程序员在开发过程中,使用文件的几 ...
- spring中添加google的guava缓存(demo)
1.pom文件中配置 <dependencies> <dependency> <groupId>org.springframework</groupId> ...
- Google的Guava类库简介(转)
说明:信息虽然有点旧,至少可以先了解个大概. Guava是一个Google的基于Java的类库集合的扩展项目,包括collections, caching, primitives support, c ...
- 有关google的guava工具包详细说明
Guava 中文是石榴的意思,该项目是 Google 的一个开源项目,包含许多 Google 核心的 Java 常用库. 目前主要包含: com.google.common.annotations c ...
- Google的Guava工具类splitter和apache stringutil对比 编辑
一直用的是apache的stringutil工具类,其实google的工具类项目 guava中居然也有字符串的分隔类splitter的,在 http://code.google.com/p/guava ...
- Guava 教程1-使用 Google Collections,Guava,static imports 编写漂亮代码
原文出处: oschina (API:http://ifeve.com/category/framework/guava-2/ JAR DOC Source 链接:http://pan.baidu.c ...
- google中guava类库:AsyncEventBus
1.guava事件总线(AsyncEventBus)使用 1.1引入依赖 <dependency> <groupId>com.google.guava</groupId& ...
- Google Guava vs Apache Commons for Argument Validation
It is an established good practice to validate method arguments at the beginning of the method body. ...
- 使用 Google Guava 美化你的 Java 代码
文章转载自:http://my.oschina.net/leejun2005/blog/172328 目录:[ - ] 1-使用 GOOGLE COLLECTIONS,GUAVA,STATIC IMP ...
随机推荐
- 【m从翻译os文章】写日志禁令Sqlnet.log和Listener.log
写日志禁令Sqlnet.log和Listener.log 参考原始: How to Disable Logging to the Sqlnet.log and the Listener.log (Do ...
- svn跨机备份
#!/bin/sh svn_bak_dir='/svndata/cloudil' svn_server='svn://172.16.40.200:9999' user=adminread pass=a ...
- Delphi透明组件开发(去掉自己的csOpaque,去掉父控件的WS_CLIPCHILDREN,增加WS_EX_TRANSPARENT,截获WM_ERASEBKGND,然后在WM_DRAWITEM里画) good
透明的控件, 一般继承自TGraphicControl的(就是那些没有handle属性, 不能有focus的控件, 如image)都有Transparent属性. 对TWinControl类的控件, ...
- Android 进行单元測试难在哪-part3
原文链接 : HOW TO MAKE OUR ANDROID APPS UNIT TESTABLE (PT. 1) 原文作者 : Matthew Dupree 译文出自 : 开发技术前线 www.de ...
- Java生成文件
Java生成文件 1.说明 以文件路径作为參数,推断该文件是否存在,若不存在就创建文件.并输出文件路径 2.实现源代码 /** * @Title:BuildFile.java * @Package:c ...
- STL的一些泛型算法
源地址:http://blog.csdn.net/byijie/article/details/8142859 从福州大学资料里摘下来的我现在能理解的泛型算法 algorithm min(a,b) 返 ...
- 从零开始,使用python快速开发web站点(1)
环境:ubuntu 12.04 python版本: 2.73 ok,首先,既然是从零开始,我们需要的是一台可以运行的python的计算机环境,并且假设你已经安装好了python, (ubuntu 或 ...
- hdu1709(母函数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1709 题意: 给你一个n,表示n个物品,下面有n个数,表示n个物品的重量,然后进行称量,每个物品只有一 ...
- 【译】ASP.NET MVC 5 教程 - 4:添加模型
原文:[译]ASP.NET MVC 5 教程 - 4:添加模型 在本节中,我们将添加一些管理电影数据库的类,这些类在ASP.NET MVC 应用程序中扮演“Model”的角色. 我们将使用.NET F ...
- WebService之Soap头验证入门
1.新建一个类,如"AuthHeaderUser",继承于"System.Web.Services.Protocols.SoapHeader"类 2.新建Web ...