Guava 库

https://www.yiibai.com/guava

https://wizardforcel.gitbooks.io/guava-tutorial/content/1.html

 

com.google.common.base.Preconditions

checkArgument(i < j, "Expected i < j, but %s > %s", i, j);
checkNotNull(a, "parameter is Null");

 

com.google.common.base.Objects

equals,避免抛出NullPointerException

Objects.equal("a", "a"); // returns true
Objects.equal(null, "a"); // returns false
Objects.equal("a", null); // returns false
Objects.equal(null, null); // returns true

hashCode, 

System.out.println(Objects.hashCode("a","b","c"));
Person person=new Person("peida",23);
System.out.println(Objects.hashCode(person));

 

com.google.common.cache.CacheBuilder

可设置过期的cache

http://www.voidcn.com/article/p-kbtzxypx-bnw.html

 

集合

http://ifeve.com/google-guava-collectionutilities/

com.google.common.collect.Maps

更简单的创建,newLinkedHashMap(),newConcurrentMap()

Map<String, String> aNewMap = Maps.newHashMap();

Lists,Sets

List<TypeThatsTooLongForItsOwnGood> list = Lists.newArrayList()

 

com.google.common.collect.Multimap

//Map<String, List<String>>
// lower -> a, b, c
// upper -> A, B

Multimap<String,String> multimap = ArrayListMultimap.create();

multimap.put("lower", "a");
multimap.put("lower", "b");
multimap.put("lower", "c");

multimap.put("upper", "A");
multimap.put("upper", "B");

Map<String, Collection<String>> map = multimap.asMap();

 

com.google.common.collect.Multiset

多个重复的值

Multiset<String> multiset = HashMultiset.create();
multiset.add("a");
multiset.add("a");
multiset.add("c");

Set<String> set = multiset.elementSet();

 

ImmutableMap,比java直接构建静态map要简单

Map<String, Integer> salary = ImmutableMap.<String, Integer> builder()
.put("John", 1000)
.put("Jane", 1500)
.put("Adam", 2000)
.put("Tom", 2000)
.build();

 

字符串

com.google.common.base.Joiner

Joiner joiner = Joiner.on("; ").skipNulls();
return joiner.join("Harry", null, "Ron", "Hermione");

com.google.common.base.Splitter

Splitter.on(‘,‘)
        .trimResults()
        .omitEmptyStrings()
        .split("foo,bar,,   qux");

 

Hash

com.google.common.hash.Hashing

md5sha256sha512sha1murmur3_128murmur3_32goodFastHash

相关文章
相关标签/搜索