您好,欢迎访问三七文档
Guava快速进阶主讲人:陈雄华目录Guava概述Strings基础类型CollectionsFunctionsandPredicatesIO处理其它Guava是什么?Google开源核心类库!快乐编程,快速编程!高效稳定,充分测试!如何获取Guava?dependencygroupIdcom.google.guava/groupIdartifactIdguava/artifactIdversion12.0/version/dependency项目网址:Maven:目录Guava概述Strings基础类型CollectionsFunctionsandPredicatesIO处理其它Joiner:合并字符串@TestpublicvoidtestJoiner(){Stringstr1=Joiner.on(;).skipNulls().join(Arrays.asList(a,b,null,c));assertEquals(str1,a;b;c);Stringstr2=Joiner.on(,).join(Arrays.asList(1,5,7));assertEquals(str2,1,5,7);}Splitter:OOP//ApacheStringUtils...String[]tokens1=StringUtils.split(one,two,three,',');//GoogleGuavasplitter...IteratableStringtokens2=Splitter.on(','),split(one,two,three);很明显,google提供的方法更加的面向对象一点,因为它要先创建一个Splitter对象,然后使用它来分割字符串,而apache的方法则有点函数式编程的味道,它的方法都是静态的。和apachecommonsStringUtils使用方法比拼Splitter:性能@TestpublicvoidtestPerformance(){finalStringnumberList=One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten;longstart=System.currentTimeMillis();for(inti=0;i1000000;i++){StringUtils.split(numberList,',');}System.out.println(System.currentTimeMillis()-start);start=System.currentTimeMillis();for(inti=0;i1000000;i++){Splitter.on(',').split(numberList);}System.out.println(System.currentTimeMillis()-start);start=System.currentTimeMillis();Splittersplitter=Splitter.on(',');for(inti=0;i1000000;i++){splitter.split(numberList);}System.out.println(System.currentTimeMillis()-start);}和apachecommonsStringUtils性能比拼apachecommons:406guava(未复用Splitter):36guava(复用Spliter):10Splitter:灵活@TestpublicvoidtestSplitter(){IterableStringarrs=Splitter.on(',').trimResults()头尾去除.omitEmptyStrings()忽略空字符串.split(,a,,b,);Stringstr2=Joiner.on(;).join(arrs);assertEquals(str2,a;b);}CharMatcher:字符串匹配器CharMatcher包括两个功能,其一是字符串匹配,其二是字符串处理,它使匹配和处理解耦了,使字符串处理的灵活性大大增强!!@TestpublicvoidtestCharMatcher(){assertEquals(CharMatcher.DIGIT.retainFrom(sometext89983andmore),89983);assertEquals(CharMatcher.DIGIT.removeFrom(sometext89983andmore),sometextandmore);assertEquals(CharMatcher.anyOf(abcxy).removeFrom(abcdefgxyz),defgz);//匹配条件复合运算Stringstr1=CharMatcher.inRange('a','f').and(CharMatcher.isNot('c')).and(CharMatcher.isNot('e')).or(CharMatcher.is('z')).replaceFrom(abyczef,*);assertEquals(str1,**yc*e*);}目录Guava概述Strings基础类型CollectionsFunctionsandPredicatesIO处理其它来看实例@TestpublicvoidtestMe(){ListIntegerintArr=Ints.asList(1,3,4,6);StringintStr=Ints.join(,,1,2,3,4);int[]concat=Ints.concat(newint[]{1,3},newint[]{4,6});intmax=Ints.max(2,3,4,7);booleancontains=Ints.contains(newint[]{1,3,4,5},6);int[]ints=Ints.toArray(Lists.newArrayList(1,2,3,5));}基础类型通用方法GuavaJDK对应GuavaJDK对应ListWrapperasList(prim...backingArray)Arrays.asListintlastIndexOf(prim[]array,primtarget)List.lastIndexOfprim[]toArray(CollectionWrappercollection)Collection.toArray()primmin(prim...array)Collections.minprim[]concat(prim[]...arrays)Iterables.concatprimmax(prim...array)Collections.maxbooleancontains(prim[]array,primtarget)Collection.containsStringjoin(Stringseparator,prim...array)Joiner.on(separator).joinintindexOf(prim[]array,primtarget)List.indexOfComparatorprim[]lexicographicalComparator()Ordering.natural().lexicographical()对应工具类基础类型对应工具类booleanBooleansbyteBytescharCharsdoubleDoublesfloatFloatsintIntslongLongsshortShorts目录Guava概述Strings基础类型CollectionsFunctionsandPredicatesIO处理其它泛型实例化:List@TestpublicvoidtestListGeneric(){ListStringlist1=newArrayListString();//原始的方式list1.add(a);list1.add(b);ListStringlist2=Lists.newArrayList();list2.add(a);list2.add(b);ListStringlist3=Lists.newArrayList(a,b);ListStringlist4=Lists.asList(a,b,newString[]{c,d});泛型实例化:Set@TestpublicvoidtestListGeneric(){ListStringlist1=newArrayListString();//原始的方式list1.add(a);list1.add(b);ListStringlist2=Lists.newArrayList();list2.add(a);list2.add(b);ListStringlist3=Lists.newArrayList(a,b);ListStringlist4=Lists.asList(a,b,newString[]{c,d});一对多之Multimap之前MapString,ListMyClassmyClassListMaptest2=newHashMapString,ListMyClass()voidputMyObject(Stringkey,Objectvalue){ListObjectmyClassList=myClassListMap.get(key);if(myClassList==null){myClassList=newArrayListobject();myClassListMap.put(key,myClassList);}myClassList.add(value);}自己实现太麻烦,你需要检查key是否存在,否则创建一个,最后代码成为这个样子:比较痛苦,如果你希望检查List中的对象是否存在,删除一个对象,或者遍历整个数据结构,那么需要更多的代码。Multimap来了MultimapString,ObjectmyMultimap=ArrayListMultimap.create();@TestpublicvoidtestMultimap(){MultimapString,StringmyMultimap=ArrayListMultimap.create();myMultimap.put(Fruits,Bannana);myMultimap.put(Fruits,Apple);myMultimap.put(Fruits,Pear);myMultimap.put(Vegetables,Carrot);intsize=myMultimap.size();System.out.println(size);//4CollectionStringfruits=myMultimap.get(Fruits);System.out.println(fruits);//[Bannana,Apple]myMultimap.remove(Fruits,Pear);System.out.println(myMultimap.get(Fruits));//[Bannana,Apple]myMultimap.removeAll(Fruits);System.out.println(myMultimap.get(Fruits));//[](EmptyCollection!)}这里需要注意,所有的guava的集合都有create()方法,这个好处就是比较简单,你不用重复泛型信息了。Multimap更多这里有一点你可能会疑惑,就是为何get方法返回的是一个collection而不是
本文标题:Guava培训
链接地址:https://www.777doc.com/doc-958464 .html