Java常用工具包-Guava
常用判断
null值
System.out.println("===========equals方法=============");System.out.println(Objects.equal(null, 'a'));//falseSystem.out.println(Objects.equal(null, null));//trueSystem.out.println(Objects.equal('a', null));//falseSystem.out.println(Objects.equal('a', 'a'));//trueSystem.out.println("===========isNullOrEmpty方法=============");System.out.println(Strings.isNullOrEmpty(null));//trueSystem.out.println(Strings.isNullOrEmpty(" "));//falseSystem.out.println(Strings.isNullOrEmpty(emptyToNull(" ")));//falseSystem.out.println(Strings.isNullOrEmpty(""));//true
另外如判断:
List<String> list = new ArrayList<String>();list.add("");list.add("null");list.add(null);list.add("zhangsan");Optional<String> possible;for (int i = 0; i < list.size(); i++) {System.out.print("真实值为:" + list.get(i) + ",emptyToNull(list.get(i))处理后的值为:" + emptyToNull(list.get(i)) + ",");possible = Optional.fromNullable(emptyToNull(list.get(i)));System.out.println("索引:" + i + ",值:" + possible.or("novalue"));}
输出:
真实值为:,emptyToNull(list.get(i))处理后的值为:null,索引:0,值:novalue
真实值为:null,emptyToNull(list.get(i))处理后的值为:null,索引:1,值:null
真实值为:null,emptyToNull(list.get(i))处理后的值为:null,索引:2,值:novalue
真实值为:zhangsan,emptyToNull(list.get(i))处理后的值为:zhangsan,索引:3,值:zhangsan
其中emptyToNull是把空字符串,转化为null。而字符串本身为”null“是正常值,为null,才是空值。
优雅的检验参数-工具类Preconditions
Object current_id = null;checkArgument(current_id != null, "当前子任务名称不能为空");
输出:
Exception in thread “main” java.lang.IllegalArgumentException: 当前子任务名称不能为空
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:134)
at com.cenrise.GuavaCheck.main(GuavaCheck.java:24)
Preconditions里面的方法:
1 .checkArgument(boolean) :
功能描述:检查boolean是否为真。 用作方法中检查参数
失败时抛出的异常类型: IllegalArgumentException
2.checkNotNull(T):
功能描述:检查value不为null, 直接返回value;
失败时抛出的异常类型:NullPointerException
3.checkState(boolean):
功能描述:检查对象的一些状态,不依赖方法参数。 例如, Iterator可以用来next是否在remove之前被调用。
失败时抛出的异常类型:IllegalStateException
4.checkElementIndex(int index, int size):
功能描述:检查index是否为在一个长度为size的list, string或array合法的范围。 index的范围区间是[0, size)(包含0不包含size)。无需直接传入list, string或array, 只需传入大小。返回index。
失败时抛出的异常类型:IndexOutOfBoundsException
5.checkPositionIndex(int index, int size):
功能描述:检查位置index是否为在一个长度为size的list, string或array合法的范围。 index的范围区间是[0, size)(包含0不包含size)。无需直接传入list, string或array, 只需传入大小。返回index。
失败时抛出的异常类型:IndexOutOfBoundsException
6.checkPositionIndexes(int start, int end, int size):
功能描述:检查[start, end)是一个长度为size的list, string或array合法的范围子集。伴随着错误信息。
失败时抛出的异常类型:IndexOutOfBoundsException
使用Joiner
典型的使用原生的java处理拼接字符串的方法:
public String buildString(List<String> stringList, String delimiter){StringBuilder builder = new StringBuilder();for (String s : stringList) {if(s !=null){builder.append(s).append(delimiter);}}builder.setLength(builder.length() – delimiter.length());return builder.toString();}
注意:这里需要移除最后一个分隔符。
原生的java实现其实也不是很复杂,但是使用Google Guava类库中的Joiner来实现的话更加简洁:Joiner.on("|").skipNulls().join(stringList);
上面的skipNulls方法是忽略了stringList中为null的空值。当然,Joiner中也可以指定空值得替代,比如如下的代码:Joiner.on("|").useForNull("no value").join(stringList);
有几点需要说明:Joiner类不仅能处理String类型的数据,还能处理数组,迭代对象以及任何对象的可变参数类。其实也是调用了Object的toString()方法。另外如果空值存在集合中,并且skipNulls活着useForNull都没有指定的话,NullPointerException异常将会抛出。还有,一旦Joiner对象创建,将会是不变的。比如如下的useForNull将不会起任何作用:
Joiner stringJoiner = Joiner.on("|").skipNulls();//the useForNull() method returns a new instance of the Joiner!stringJoiner.useForNull("missing");stringJoiner.join("foo","bar",null);
Joiner类不仅可以返回String对象,还可以返回StringBuilder对象:
StringBuilder stringBuilder = new StringBuilder();Joiner joiner = Joiner.on("|").skipNulls();//returns the StringBuilder instance with the values foo,bar,baz appeneded with "|" delimitersjoiner.appendTo(stringBuilder,"foo","bar","baz")
Joiner类可以作用于实现了Appendable的所有类:
FileWriter fileWriter = new FileWriter(new File("path")):List<Date> dateList = getDates();Joiner joiner = Joiner.on("#").useForNulls(" ");//returns the FileWriter instance with the valuesappended into itjoiner.appendTo(fileWriter,dateList);
Joiner类也可以作用于Map对象:
public void testMapJoiner() {//Using LinkedHashMap so that the original order is preservedString expectedString = "Washington D.C=Redskins#New York City=Giants#Philadelphia=Eagles#Dallas=Cowboys";Map<String,String> testMap = Maps.newLinkedHashMap();testMap.put("Washington D.C","Redskins");testMap.put("New York City","Giants");testMap.put("Philadelphia","Eagles");testMap.put("Dallas","Cowboys");String returnedString = Joiner.on("#").withKeyValueSeparator("=").join(testMap);assertThat(returnedString,is(expectedString));}
字段串操作
模块部分内容来源自:google guava中定义的String操作
public class GuavaString {/*** 字符串是否为空*/@Testpublic void testIsNullOrEmpty() {String input = "";boolean isNullOrEmpty = Strings.isNullOrEmpty(input);System.out.println("输入值 " + (isNullOrEmpty ? "是" : "不是") + " null or empty.");}/*** 获得两个字符串相同的前缀或者后缀*/@Testpublic void testCommonPrefixSuffix() {String a = "com.jd.coo.Hello";String b = "com.jd.coo.Hi";String ourCommonPrefix = Strings.commonPrefix(a, b);//a,b 相同的前缀是: com.jd.coo.HSystem.out.println("a,b 相同的前缀是: " + ourCommonPrefix);String c = "com.google.Hello";String d = "com.jd.Hello";String ourSuffix = Strings.commonSuffix(c, d);//c,d 相同的后缀是: .HelloSystem.out.println("c,d 相同的后缀是: " + ourSuffix);}/*** Strings的padStart和padEnd方法来补全字符串*/@Testpublic void testpadStartEnd() {int minLength = 4;//在后面补完,4位。如前边是12,补全4位,使用0,应该是1200;如果是123,补全4位是1230.前边补全类似String padEndResult = Strings.padEnd("123", minLength, '0');System.out.println("padEndResult is " + padEndResult);String padStartResult = Strings.padStart("1", 2, '0');System.out.println("padStartResult is " + padStartResult);}/*** 使用Splitter类来拆分字符串* Splitter类可以方便的根据正则表达式来拆分字符串,可以去掉拆分结果中的空串,可以对拆分后的字串做trim操作,还可以做二次拆分。* 我们先看一个基本的拆分例子:*/@Testpublic void testSplitter() {//JDKString splitterStr = ",a,,b,";String[] strArray = splitterStr.split(",");//length:4System.out.println("length:" + strArray.length);//GUAVAList<String> listStr = Splitter.on(',').trimResults().omitEmptyStrings().splitToList(splitterStr);//length:[a, b]System.out.println("length:" + listStr);String str = "xiaoming=11,xiaohong=23";Map<String, String> map = Splitter.on(",").withKeyValueSeparator("=").split(str);//{xiaoming=11, xiaohong=23}System.out.println(map);//GUAVA,Splitter的onPattern方法传入的是一个正则表达式,其后紧跟的trimResults()方法表示要对结果做trim,omitEmptyStrings()表示忽略空字符串,split方法会执行拆分操作。Iterable<String> splitResults = Splitter.onPattern("[,,]{1,}").trimResults().omitEmptyStrings().split("hello,word,,世界,水平");for (String item : splitResults) {//分别输出:hello、word、世界、水平System.out.println(item);}//split返回的结果为Iterable<String>,我们可以使用for循环语句来逐个打印拆分字符串的结果。//Splitter还有更强大的功能,做二次拆分,这里二次拆分的意思是拆分两次,例如我们可以将a=b;c=d这样的字符串拆分成一个Map<String,String>。//二次拆分首先是使用onPattern做第一次的拆分,然后再通过withKeyValueSeperator('')方法做第二次的拆分。String toSplitString = "a=b;c=d,e=f";Map<String, String> kvs = Splitter.onPattern("[,;]{1,}").withKeyValueSeparator('=').split(toSplitString);for (Map.Entry<String, String> entry : kvs.entrySet()) {System.out.println(String.format("%s=%s", entry.getKey(), entry.getValue()));}}/*** 有拆分字符串必然就有合并字符串,guava为我们提供了Joiner类来做字符串的合并* Splitter类可以方便的根据正则表达式来拆分字符串,可以去掉拆分结果中的空串,可以对拆分后的字串做trim操作,还可以做二次拆分。* 我们先看一个基本的拆分例子:*/@Testpublic void testJoiner() {String joinResult = Joiner.on(" ").join(new String[]{"hello", "world"});System.out.println(joinResult);// 上面例子中我们使用Joiner.on(" ").join(xx)来合并字符串。很简单也很有效。//// Splitter方法可以对字符串做二次的拆分,对应的Joiner也可以逆向操作,将Map<String,String>做合并。我们看下下面的例子:String toSplitString = "a=b;c=d,e=f";Map<String, String> kvs = Splitter.onPattern("[,;]{1,}").withKeyValueSeparator('=').split(toSplitString);for (Map.Entry<String, String> entry : kvs.entrySet()) {System.out.println(String.format("%s=%s", entry.getKey(), entry.getValue()));}Map<String, String> map = new HashMap<String, String>();map.put("a", "b");map.put("c", "d");String mapJoinResult = Joiner.on(",").withKeyValueSeparator("=").join(kvs);System.out.println(mapJoinResult);// 使用withKeyValueSeparator方法可以对map做合并。合并的结果是:a=b,c=d}/*** 大小写转换* CaseFormat是一种实用工具类,以提供不同的ASCII字符格式之间的转换。* UPPER_CAMEL,即我们常说的"驼峰式"编写方式;其次,我们常用的是:UPPER_UNDERSCORE,即我们常用的常量命名法,*/@Testpublic void testCaseFormat() {//testDataSystem.out.println(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, "test-data"));//testDataSystem.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "test_data"));//TestDataSystem.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "test_data"));//testdataSystem.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "testdata"));//test_dataSystem.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "TestData"));//test-dataSystem.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, "testData"));}/*** 字符匹配*/@Testpublic void testCharMatcher() {// 移除字符串中的数字String str = CharMatcher.DIGIT.removeFrom("ac2dgc45dbg4");System.out.println(str);// 只保留字符串中的数字String str1 = CharMatcher.DIGIT.retainFrom("ac2dgc45dbg4");System.out.println(str1);//使用*替换字符串中的数字String str2 = CharMatcher.DIGIT.replaceFrom("ac2dgc45dbg4", "*");System.out.println(str2);// 移除字符串中a-z的所有小写字母String str3 = CharMatcher.inRange('1', 'z').removeFrom("0a0bcz8xy5mDgilBpoDyegA");System.out.println(str3);}}
[TOC]
缓存,在我们日常开发中是必不可少的一种解决性能问题的方法。简单的说,cache 就是为了提升系统性能而开辟的一块内存空间。
缓存的主要作用是暂时在内存中保存业务系统的数据处理结果,并且等待下次访问使用。在日常开发的很多场合,由于受限于硬盘IO的性能或者我们自身业务系统的数据处理和获取可能非常费时,当我们发现我们的系统这个数据请求量很大的时候,频繁的IO和频繁的逻辑处理会导致硬盘和CPU资源的瓶颈出现。缓存的作用就是将这些来自不易的数据保存在内存中,当有其他线程或者客户端需要查询相同的数据资源时,直接从缓存的内存块中返回数据,这样不但可以提高系统的响应时间,同时也可以节省对这些数据的处理流程的资源消耗,整体上来说,系统性能会有大大的提升。
缓存在很多系统和架构中都用广泛的应用,例如:
1.CPU缓存
2.操作系统缓存
3.本地缓存
4.分布式缓存
5.HTTP缓存
6.数据库缓存
等等,可以说在计算机和网络领域,缓存无处不在。可以这么说,只要有硬件性能不对等,涉及到网络传输的地方都会有缓存的身影。
Guava Cache(本地缓存)
Guava Cache是一个全内存的本地缓存实现,它提供了线程安全的实现机制。整体上来说Guava cache 是本地缓存的不二之选,简单易用,性能好。
Guava Cache有两种创建方式:
- cacheLoader
- callable callback
通过这两种方法创建的cache,和通常用map来缓存的做法比,不同在于,这两种方法都实现了一种逻辑——从缓存中取key X的值,如果该值已经缓存过了,则返回缓存中的值,如果没有缓存过,可以通过某个方法来获取这个值。但不同的在于cacheloader的定义比较宽泛,是针对整个cache定义的,可以认为是统一的根据key值load value的方法。而callable的方式较为灵活,允许你在get的时候指定。
cacheLoader方式实现实例:
LoadingCache<String, String> cahceBuilder = CacheBuilder.newBuilder().build(new CacheLoader<String, String>() {@Overridepublic String load(String key) throws Exception {String strProValue = "hello " + key + "!";return strProValue;}});System.out.println("jerry value:" + cahceBuilder.apply("jerry"));System.out.println("jerry value:" + cahceBuilder.get("jerry"));System.out.println("peida value:" + cahceBuilder.get("peida"));System.out.println("peida value:" + cahceBuilder.apply("peida"));System.out.println("lisa value:" + cahceBuilder.apply("lisa"));cahceBuilder.put("harry", "ssdded");System.out.println("harry value:" + cahceBuilder.get("harry"));
输出:
jerry value:hello jerry!
jerry value:hello jerry!
peida value:hello peida!
peida value:hello peida!
lisa value:hello lisa!
harry value:ssdded
callable callback的实现:
/*** callable callback的实现** @throws Exception*/@Testpublic void testcallableCache() throws Exception {Cache<String, String> cache = CacheBuilder.newBuilder().maximumSize(1000).build();String resultVal = cache.get("jerry", new Callable<String>() {public String call() {String strProValue = "hello " + "jerry" + "!";return strProValue;}});System.out.println("jerry value : " + resultVal);resultVal = cache.get("peida", new Callable<String>() {public String call() {String strProValue = "hello " + "peida" + "!";return strProValue;}});System.out.println("peida value : " + resultVal);}
输出:
jerry value : hello jerry!
peida value : hello peida!
cache的参数说明:
回收的参数:
- 大小的设置:CacheBuilder.maximumSize(long) CacheBuilder.weigher(Weigher) CacheBuilder.maxumumWeigher(long)
- 时间:expireAfterAccess(long, TimeUnit) expireAfterWrite(long, TimeUnit)
- 引用:CacheBuilder.weakKeys() CacheBuilder.weakValues() CacheBuilder.softValues()
- 明确的删除:invalidate(key) invalidateAll(keys) invalidateAll()
- 删除监听器:CacheBuilder.removalListener(RemovalListener)
refresh机制: - LoadingCache.refresh(K) 在生成新的value的时候,旧的value依然会被使用。
- CacheLoader.reload(K, V) 生成新的value过程中允许使用旧的value
- CacheBuilder.refreshAfterWrite(long, TimeUnit) 自动刷新cache
基于泛型的实现:
/*** 不需要延迟处理(泛型的方式封装)** @return*/public <K, V> LoadingCache<K, V> cached(CacheLoader<K, V> cacheLoader) {LoadingCache<K, V> cache = CacheBuilder.newBuilder().maximumSize(2).weakKeys().softValues().refreshAfterWrite(120, TimeUnit.SECONDS).expireAfterWrite(10, TimeUnit.MINUTES).removalListener(new RemovalListener<K, V>() {@Overridepublic void onRemoval(RemovalNotification<K, V> rn) {System.out.println(rn.getKey() + "被移除");}}).build(cacheLoader);return cache;}/*** 通过key获取value* 调用方式 commonCache.get(key) ; return String** @param key* @return* @throws Exception*/public LoadingCache<String, String> commonCache(final String key) throws Exception {LoadingCache<String, String> commonCache = cached(new CacheLoader<String, String>() {@Overridepublic String load(String key) throws Exception {return "hello " + key + "!";}});return commonCache;}@Testpublic void testCache() throws Exception {LoadingCache<String, String> commonCache = commonCache("peida");System.out.println("peida:" + commonCache.get("peida"));commonCache.apply("harry");System.out.println("harry:" + commonCache.get("harry"));commonCache.apply("lisa");System.out.println("lisa:" + commonCache.get("lisa"));}
输出:
peida:hello peida!
harry:hello harry!
peida被移除
lisa:hello lisa!
基于泛型的Callable Cache实现:
private static Cache<String, String> cacheFormCallable = null;/*** 对需要延迟处理的可以采用这个机制;(泛型的方式封装)* @param <K>* @param <V>* @param key* @param callable* @return V* @throws Exception*/public static <K,V> Cache<K , V> callableCached() throws Exception {Cache<K, V> cache = CacheBuilder.newBuilder().maximumSize(10000).expireAfterWrite(10, TimeUnit.MINUTES).build();return cache;}private String getCallableCache(final String userName) {try {//Callable只有在缓存值不存在时,才会调用return cacheFormCallable.get(userName, new Callable<String>() {@Overridepublic String call() throws Exception {System.out.println(userName+" from db");return "hello "+userName+"!";}});} catch (ExecutionException e) {e.printStackTrace();return null;}}@Testpublic void testCallableCache() throws Exception{final String u1name = "peida";final String u2name = "jerry";final String u3name = "lisa";cacheFormCallable=callableCached();System.out.println("peida:"+getCallableCache(u1name));System.out.println("jerry:"+getCallableCache(u2name));System.out.println("lisa:"+getCallableCache(u3name));System.out.println("peida:"+getCallableCache(u1name));}
输出:
peida from db
peida:hello peida!
jerry from db
jerry:hello jerry!
lisa from db
lisa:hello lisa!
peida:hello peida!
说明:Callable只有在缓存值不存在时,才会调用,比如第二次调用getCallableCache(u1name)直接返回缓存中的值
guava Cache数据移除
guava做cache时候数据的移除方式,在guava中数据的移除分为被动移除和主动移除两种。
被动移除数据的方式,guava默认提供了三种方式:
1.基于大小的移除:看字面意思就知道就是按照缓存的大小来移除,如果即将到达指定的大小,那就会把不常用的键值对从cache中移除。
定义的方式一般为 CacheBuilder.maximumSize(long),还有一种一种可以算权重的方法,个人认为实际使用中不太用到。就这个常用的来看有几个注意点,
其一,这个size指的是cache中的条目数,不是内存大小或是其他;
其二,并不是完全到了指定的size系统才开始移除不常用的数据的,而是接近这个size的时候系统就会开始做移除的动作;
其三,如果一个键值对已经从缓存中被移除了,你再次请求访问的时候,如果cachebuild是使用cacheloader方式的,那依然还是会从cacheloader中再取一次值,如果这样还没有,就会抛出异常
2.基于时间的移除:guava提供了两个基于时间移除的方法
expireAfterAccess(long, TimeUnit) 这个方法是根据某个键值对最后一次访问之后多少时间后移除
expireAfterWrite(long, TimeUnit) 这个方法是根据某个键值对被创建或值被替换后多少时间移除
3.基于引用的移除:
这种移除方式主要是基于java的垃圾回收机制,根据键或者值的引用关系决定移除
主动移除数据方式,主动移除有三种方法:
1.单独移除用 Cache.invalidate(key)
2.批量移除用 Cache.invalidateAll(keys)
3.移除所有用 Cache.invalidateAll()
如果需要在移除数据的时候有所动作还可以定义Removal Listener,但是有点需要注意的是默认Removal Listener中的行为是和移除动作同步执行的,如果需要改成异步形式,可以考虑使用RemovalListeners.asynchronous(RemovalListener, Executor)
一、只读设置
package Guava;import java.util.ArrayList;import java.util.Collections;import java.util.List;import com.google.common.collect.ImmutableList;/*** 只读设置*/public class Demo01 {public static void main(String[] args) {List<String> list = new ArrayList<>();list.add("a");list.add("b");list.add("c");//对原有的List进行包装,相当于原有List的视图,快照,不够安全.List<String> readList = Collections.unmodifiableList(list);//java.lang.UnsupportedOperationException//readList.add("d");//报错list.add("d");//改变原有List 视图也一起改变 不报错System.out.println(readList);//Guava//对比查看 初始化List guava对只读设置安全可靠 并且相对简单List<String> immutableList = ImmutableList.of("a","b","c");//java.lang.UnsupportedOperationException//immutableList.add("d");//报错System.out.println(immutableList);}}
运行结果
[a, b, c, d]
[a, b, c]
二、函数式编程
package Guava;import java.text.SimpleDateFormat;import java.util.Collection;import java.util.List;import java.util.Set;import com.google.common.base.Function;import com.google.common.base.Functions;import com.google.common.base.Predicate;import com.google.common.collect.Collections2;import com.google.common.collect.Lists;import com.google.common.collect.Sets;/*** 函数式编程:解耦* 1、Predicate 断言* 2、Function* 函数的组合式编程* Functions.compose(f1, f2);* 工具: Collections2.filter 过滤器* Collections2.transform 转换*/public class Demo02 {public static void main(String[] args) {test1();//过滤器System.out.println("----------------");test2();System.out.println("----------------");test3();}//过滤器public static void test1(){//创建List 静态初始化List<String> list = Lists.newArrayList("SDF","SDAF","FASD","MOOM","ESDSE");//找出回文//匿名内部类对象:匿名内部类,同时创建类对象Collection<String> palindroomList = Collections2.filter(list, new Predicate<String>() {public boolean apply(String input){//业务逻辑return new StringBuilder(input).reverse().toString().equals(input);}});for(String temp:palindroomList){System.out.println(temp);}}//转换public static void test2(){//类型转换Set<Long> timeSet = Sets.newHashSet();timeSet.add(1000000L);timeSet.add(999999999999L);timeSet.add(20000000L);Collection<String> timeStrCol = Collections2.transform(timeSet, new Function<Long, String>() {@Overridepublic String apply(Long input) {return new SimpleDateFormat("yyyy-MM-dd").format(input);}});for(String temp:timeStrCol){System.out.println(temp);}}//组合式函数编程public static void test3(){//确保容器中的字符串长度不超过5,超过进行截取,后全部大写List<String> list = Lists.newArrayList("lovebaby","good","happiness");//确保容器中的字符串长度不超过5,超过进行截取Function<String, String> f1 = new Function<String, String>() {@Overridepublic String apply(String input) {return input.length()>5?input.substring(0,5):input;}};//全部大写Function<String, String> f2 = new Function<String, String>() {@Overridepublic String apply(String input) {return input.toUpperCase();}};//组合使用//String = f2(f1(String))Function<String, String> f = Functions.compose(f1, f2);Collection<String> resultCol = Collections2.transform(list,f);for(String temp:resultCol){System.out.println(temp);}}}
运行结果:
MOOM
ESDSE
1970-01-01
1970-01-01
2001-09-09
LOVEB
GOOD
HAPPI
三、约束条件
package Guava;import static com.google.common.base.Preconditions.checkNotNull;import java.util.Collection;import java.util.Set;import com.google.common.collect.ForwardingSet;import com.google.common.collect.Lists;/*** 解决guava-18.0.jar不能使用* com.google.common.collect.Constraints、* com.google.common.collect.Constraint 的问题。* @author liguodong* @param <E>*/interface Constraint<E>{//public String checkElement(String element);E checkElement(E element);}class Constraints<E>{public static <E> Set<E> constrainedSet(Set<E> set, Constraint<? super E> constraint) {return new ConstrainedSet<E>(set, constraint);}private static <E> Collection<E> checkElements(Collection<E> elements, Constraint<? super E> constraint) {Collection<E> copy = Lists.newArrayList(elements);for (E element : copy) {constraint.checkElement(element);}return copy;}/** @see Constraints#constrainedSet */static class ConstrainedSet<E> extends ForwardingSet<E> {private final Set<E> delegate;private final Constraint<? super E> constraint;public ConstrainedSet(Set<E> delegate, Constraint<? super E> constraint) {this.delegate = checkNotNull(delegate);this.constraint = checkNotNull(constraint);}@Override protected Set<E> delegate() {return delegate;}@Override public boolean add(E element) {constraint.checkElement(element);return delegate.add(element);}@Override public boolean addAll(Collection<? extends E> elements) {return delegate.addAll(checkElements(elements, constraint));}}}
package Guava;import java.util.Set;import com.google.common.base.Preconditions;//import com.google.common.collect.Constraint;//import com.google.common.collect.Constraints;import com.google.common.collect.Sets;/*** 加入约束条件:非空,长度验证* Constraint* Precondiotions* Constrains*/public class Demo03 {public static void main(String[] args) {Set<String> sets = Sets.newHashSet();//创建约束Constraint<String> constraint = new Constraint<String>() {@Overridepublic String checkElement(String element) {//非空验证Preconditions.checkNotNull(element);//长度验证5-20Preconditions.checkArgument(element.length()>=5&&element.length()<=20);return element;}};Set<String> cs = Constraints.constrainedSet(sets, constraint);//cs.add(null);// java.lang.NullPointerException//cs.add("doog");//java.lang.IllegalArgumentExceptioncs.add("liguodong");for(String temp:cs){System.out.println(temp);}}}
运行结果
liguodong
四、集合的操作
package Guava;import java.util.Set;import com.google.common.collect.Sets;import com.google.common.collect.Sets.SetView;/*** 集合的操作:交集,差集,并集* Sets.intersection()* Sets.difference()* Sets.union()*/public class Demo04 {public static void main(String[] args) {Set<Integer> sets = Sets.newHashSet(1,2,3,4,5,6);Set<Integer> sets2 = Sets.newHashSet(3,4,5,6,7,8,9);//交集System.out.println("交集为:");SetView<Integer> intersection = Sets.intersection(sets, sets2);for(Integer temp:intersection){System.out.print(temp+" ");}System.out.println();//差集System.out.println("差集为:");SetView<Integer> diff = Sets.difference(sets, sets2);for(Integer temp:diff){System.out.print(temp+" ");}System.out.println();//并集System.out.println("并集为:");SetView<Integer> union = Sets.union(sets, sets2);for(Integer temp:union){System.out.print(temp+" ");}System.out.println();}}
运行结果
交集为:
3 4 5 6
差集为:
1 2
并集为:
1 2 3 4 5 6 7 8 9
五、MultiSet
package Guava;import java.util.Set;import com.google.common.collect.HashMultiset;import com.google.common.collect.Multiset;/*** 统计单词出现的次数* 1.HashMap 分拣存储+面向对象思维-->判断* 2.MultiSet:无序+可重复 count()方法获取单词的次数 增强了可读性+操作简单*/public class Demo05 {public static void main(String[] args) {String str = "this is a cat that is a mice where is the food";//分割字符串String[] strArray = str.split(" ");//存储到Multiset中Multiset<String> set = HashMultiset.create();for(String temp :strArray){set.add(temp);}//获取所有的单词SetSet<String> letters = set.elementSet();for(String temp:letters){System.out.println(temp+"-->"+set.count(temp));}}}
运行结果
mice—>1
that—>1
cat—>1
is—>3
food—>1
a—>2
the—>1
where—>1
this—>1
六、Multimap
package Guava;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;import com.google.common.collect.ArrayListMultimap;import com.google.common.collect.Multimap;/*** 分析查看 教师 教授 的每门课程* Multimap:key-value key可以重复*/public class Demo06 {public static void main(String[] args) {Map<String,String> cours = new HashMap<>();//加入测试数据cours.put("改革开放","邓小平");cours.put("三个代表","江泽民");cours.put("和谐社会","胡锦涛");cours.put("八荣八耻","胡锦涛");cours.put("互联网+","李克强");//MultimapMultimap<String, String> teachers = ArrayListMultimap.create();//迭代器Iterator<Map.Entry<String, String>> it = cours.entrySet().iterator();while(it.hasNext()){Map.Entry<String, String> entry = it.next();String key = entry.getKey();//课程String value = entry.getValue();//教师//教师-->课程teachers.put(value,key);}//查看MultimapSet<String> keyset = teachers.keySet();for(String key:keyset){Collection<String> col = teachers.get(key);System.out.println(key+"-->"+col);}}}
运行结果
邓小平—>[改革开放]
江泽民—>[三个代表]
胡锦涛—>[八荣八耻, 和谐社会]
李克强—>[互联网+]
七、BiMap
package Guava;import com.google.common.collect.BiMap;import com.google.common.collect.HashBiMap;/*** HashMap 键唯一,值可以重复* BiMap:双向Map(Bidirectional Map) 键与值都不能重复(unique -valued map)*/public class Demo07 {public static void main(String[] args) {BiMap<String, String> biMap = HashBiMap.create();biMap.put("liguodong", "liguodong@sina.com");biMap.put("good","good@qq.com");//通过邮箱找用户String user = biMap.inverse().get("good@qq.com");System.out.println(user);System.out.println( biMap.inverse().inverse()==biMap );}}
运行结果
good
true
八、双键的Map
package Guava;import java.util.Map;import java.util.Set;import com.google.common.collect.HashBasedTable;import com.google.common.collect.Table;import com.google.common.collect.Table.Cell;import com.google.common.collect.Tables;/*** 双键的Map--> Table-->rowKey+columnKey+value* 1.方法* 所有的行数据:cellSet()* 所有的学生:rowKeySet()* 所有的课程:columnKeySet()* 所有的成绩:values()* 学生对应的课程:rowMap()+get(学生)* row(学生)** 课程对应的学生:columnMap()+get(课程)* column(课程)*/public class Demo08 {public static void main(String[] args) {Table<String, String, Integer> tables = HashBasedTable.create();//测试数据tables.put("a", "javase", 80);tables.put("b", "javase", 90);tables.put("a", "oracle", 100);tables.put("c", "javase", 95);//所有的行数据Set<Cell<String, String, Integer>> cells = tables.cellSet();for(Cell<String, String, Integer> temp:cells){System.out.println(temp.getRowKey()+"-->"+temp.getColumnKey()+"-->"+temp.getValue());}System.out.println("=======学生查看成绩============");System.out.print("学生\t");//所有的课程Set<String> cours = tables.columnKeySet();for(String t:cours){System.out.print(t+"\t");}System.out.println();//所有的学生Set<String> stus = tables.rowKeySet();for(String stu:stus){System.out.print(stu+"\t");//输出学生//以下是输出学生的每一门课程Map<String,Integer> scores = tables.row(stu);//<课程,分数>for(String c:cours)//课程{System.out.print(scores.get(c)+"\t");}System.out.println();}System.out.println("=======课程查看成绩============");System.out.print("课程\t");//所有的学生Set<String> stus1 = tables.rowKeySet();for(String t:stus1){System.out.print(t+"\t");}System.out.println();//所有的课程Set<String> cours1 = tables.columnKeySet();for(String c:cours1){System.out.print(c+"\t");//课程Map<String,Integer> scores = tables.column(c);//<学生,分数>for(String s:stus1){System.out.print(scores.get(s)+"\t");}System.out.println();}System.out.println("=======转换===========");Table<String, String, Integer> tables2 = Tables.transpose(tables);//所有的行数据Set<Cell<String, String, Integer>> cells2 = tables2.cellSet();for(Cell<String, String, Integer> temp:cells2){System.out.println(temp.getRowKey()+"-->"+temp.getColumnKey()+"-->"+temp.getValue());}}}
运行结果
b—>javase—>90
c—>javase—>95
a—>oracle—>100
a—>javase—>80
=学生查看成绩======
学生 javase oracle
b 90 null
c 95 null
a 80 100
=课程查看成绩======
课程 b c a
javase 90 95 80
oracle null null 100
=转换=====
javase—>b—>90
javase—>c—>95
oracle—>a—>100
javase—>a—>80
