Guava工具类简介
在开发中,工具类的使用避免上重复造轮子,极大的节省了开发时间,提高了工作效率,Google公司提供的Guava工具类,高效设计良好的API,遵循高效的java语法,使代码更加简洁。
使用Maven引入依赖
<dependency
>
<groupId
>com
.google
.guava
</groupId
>
<artifactId
>guava
</artifactId
>
<version
>20.0</version
>
</dependency
>
具体使用
1、集合的创建
List
<String
> list
= Lists
.newArrayList();
Set
<String
> set = Sets
.newHashSet();
Map
<String
, String
> map
= Maps
.newHashMap();
ImmutableList
<String
> list
= ImmutableList
.of("v1","v2");
ImmutableSet
<String
> set = ImmutableSet
.of("v1","v2");
ImmutableMap
<String
, String
> map
= new ImmutableMap.Builder()
.put("k1", "v1")
.put("k2", "v2")
.build();
Multiset
<String
> mulSet
= HashMultiset
.create();
Multimap
<String
, String
> mulMap
= ArrayListMultimap
.create();
BiMap
<String
, String
> biMap
= HashBiMap
.create();
Table
<String
, String
, Integer
> tables
= HashBasedTable
.create();
2、集合的使用
String result
= Joiner
.on("-").join(list
);
String result
= Joiner
.on(",").withKeyValueSeparator("=").join(map
);
List
<String
> list
= Splitter
.on("-").splitToList(str
);
Map
<String
,String
> map
= Splitter
.on(",").withKeyValueSeparator("=").split(str
);
List
<String
> list
= Splitter
.on("-").omitEmptyStrings().trimResults().splitToList(str
);
List
<String
> list
= Splitter
.on("-|,|=").omitEmptyStrings().trimResults().splitToList(str
);
boolean result
= CharMatcher
.inRange('a', 'z').or(CharMatcher
.inRange('A', 'Z')).matches(str
);
String numStr
= CharMatcher
.digit().retainFrom("abc 123 qwe");
String str
= CharMatcher
.digit().removeFrom("abc 123 qwe");
3、文件操作
File file
= new File("/value.txt");
List
<String
> list
= null;
try {
list
= Files
.readLines(file
, Charsets
.UTF_8);
} catch (Exception e
) {
}
Files
.copy(from,to
);
Files
.deleteDirectoryContents(File directory
);
Files
.deleteRecursively(File file
);
Files
.move(File
from, File to
);
URL url
= Resources
.getResource("abc.xml");
4、Ordering排序器
natural() 对可排序类型做自然排序,如数字按大小,日期按先后排序
usingToString() 按对象的字符串形式做字典排序[lexicographical ordering]
from(Comparator) 把给定的Comparator转化为排序器
reverse() 获取语义相反的排序器
nullsFirst() 使用当前排序器,但额外把null值排到最前面。
nullsLast() 使用当前排序器,但额外把null值排到最后面。
compound(Comparator) 合成另一个比较器,以处理当前排序器中的相等情况。
lexicographical() 基于处理类型T的排序器,返回该类型的可迭代对象Iterable<T>的排序器。
onResultOf(Function) 对集合中元素调用Function,再按返回值用当前排序器排序。
Person person
= new Person("aa",14);
Person ps
= new Person("bb",13);
Ordering
<Person
> byOrdering
= Ordering
.natural().nullsFirst().onResultOf(new Function<Person
,String
>(){
public String
apply(Person person
){
return person
.age
.toString();
}
});
byOrdering
.compare(person
, ps
);
System
.out
.println(byOrdering
.compare(person
, ps
));
5、其它操作
Stopwatch stopwatch
= Stopwatch
.createStarted();
long nanos
= stopwatch
.elapsed(TimeUnit
.MILLISECONDS);
System
.out
.println(nanos
);
Person person
= new Person("aa",11);
String str
= MoreObjects
.toStringHelper("Person").add("age", person
.getAge()).toString();
System
.out
.println(str
);
SetView union
= Sets
.union(setA
, setB
);
SetView difference
= Sets
.difference(setA
, setB
);
SetView intersection
= Sets
.intersection(setA
, setB
);
MapDifference differenceMap
= Maps
.difference(mapA
, mapB
);
differenceMap
.areEqual();
Map entriesDiffering
= differenceMap
.entriesDiffering();
Map entriesOnlyOnLeft
= differenceMap
.entriesOnlyOnLeft();
Map entriesOnlyOnRight
= differenceMap
.entriesOnlyOnRight();
Map entriesInCommon
= differenceMap
.entriesInCommon();