更详细整理: http://blog.csdn.net/alecf/article/details/24775979
原文: http://my.oschina.net/shma1664/blog/596904
原文: http://my.oschina.net/shma1664/blog/596904
源码包的简单说明:
com.google.common.annotations:普通注解类型。
com.google.common.base:基本工具类库和接口。
com.google.common.cache:缓存工具包,非常简单易用且功能强大的JVM内缓存。
com.google.common.collect:带泛型的集合接口扩展和实现,以及工具类,这里你会发现很多好玩的集合。
com.google.common.eventbus:发布订阅风格的事件总线。
com.google.common.hash: 哈希工具包。
com.google.common.io:I/O工具包。
com.google.common.math:原始算术类型和超大数的运算工具包。
com.google.common.net:网络工具包。
com.google.common.primitives:八种原始类型和无符号类型的静态工具包。
com.google.common.reflect:反射工具包。
com.google.common.util.concurrent:多线程工具包。
com.google.common.annotations:普通注解类型。
com.google.common.base:基本工具类库和接口。
com.google.common.cache:缓存工具包,非常简单易用且功能强大的JVM内缓存。
com.google.common.collect:带泛型的集合接口扩展和实现,以及工具类,这里你会发现很多好玩的集合。
com.google.common.eventbus:发布订阅风格的事件总线。
com.google.common.hash: 哈希工具包。
com.google.common.io:I/O工具包。
com.google.common.math:原始算术类型和超大数的运算工具包。
com.google.common.net:网络工具包。
com.google.common.primitives:八种原始类型和无符号类型的静态工具包。
com.google.common.reflect:反射工具包。
com.google.common.util.concurrent:多线程工具包。
1、基本功能1)字符串操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
Java代码 package com.shma.guava.base; import java.util.Map; import org.junit.Test; import com.google.common.base.Joiner; import com.google.common.base.Splitter; import com.google.common.base.Strings; import com.google.common.collect.Maps; /** * 字符串处理 * @author admin * */ public class StringsTest { /** * 判断是否为空 */ @Test public void testIsNullOrEmpty() { String name = ""; System.out.println(Strings.isNullOrEmpty(name)); //true String name2 = null; System.out.println(Strings.isNullOrEmpty(name2)); //true String name3 = "shma"; System.out.println(Strings.isNullOrEmpty(name3)); //false } /** * 截取两个字符串的相同前缀 */ @Test public void testCommonPrefix() { // 两个字符串相同的前缀或者后缀 String aString = "hi,a.shma.hello"; String bString = "hi,b.jjq.hello"; System.out.println(Strings.commonPrefix(aString, bString)); //hi, } /** * 截取两个字符串的相同后缀 */ @Test public void testCommonSuffix() { // 两个字符串相同的前缀或者后缀 String aString = "hi,a.shma.hello"; String bString = "hi,b.jjq.hello"; System.out.println(Strings.commonSuffix(aString, bString)); //.hello } /** * 字符串补全 */ @Test public void testPad() { int minLength = 5; //末尾以0补全 String padEndResult = Strings.padEnd("123", minLength, '0'); System.out.println(padEndResult); //12300 //开始补全 String padStartResult = Strings.padStart("123", minLength, '0'); System.out.println(padStartResult); //00123 } /** * 拆分字符串 */ @Test public void testSplitter() { Iterable<String> iterable = Splitter.onPattern("[,,;]") .trimResults() .omitEmptyStrings() .split("马韶华,张琦,笑笑,,老李,类型 哈哈,非也; 宵夜 "); for(String item : iterable) { System.out.println(item); } //二次拆分 String toSplitString = "a=1; b=2, c=3"; Map<String, String> kvs = Splitter.onPattern("[;,]") .trimResults() .omitEmptyStrings() .withKeyValueSeparator("=") .split(toSplitString); System.out.println(kvs); //{a=1, b=2, c=3} } /** * 字符串合并 */ @Test public void testJoin() { String users = Joiner.on(",").join(new String[]{"张三", "李四", "王五"}); System.out.println(users); //张三,李四,王五 //将Map<String, String>合并 Map<String, String> dataMap = Maps.newHashMap(); dataMap.put("1001", "张三"); dataMap.put("1002", "李四"); dataMap.put("1003", "王五"); dataMap.put("1004", "马六"); String mapString = Joiner.on(",").withKeyValueSeparator("=").join(dataMap); System.out.println(mapString); // 1003=王五,1004=马六,1001=张三,1002=李四 } /** * 重复输出次数 */ @Test public void testRepeat() { System.out.println(Strings.repeat("1234", 2)); // 12341234 } } |
2)对象封装:Objects
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
Java代码 package com.shma.guava2.base; import org.junit.Test; import com.google.common.base.Objects; import com.google.common.collect.ComparisonChain; /** * 复写Object中的方法实现 * @author admin * */ public class ObjectsTest { /** * 比较大小 */ @Test public void compareTest() { Person person1 = new Person("jqq", 24); Person person2 = new Person("jqq", 28); Person person3 = new Person("shma", 24); Person person4 = new Person("shma", 21); Person person5 = new Person("shma", 21); System.out.println(person1.compareTo(person2)); System.out.println(person1.compareTo(person3)); System.out.println(person3.compareTo(person4)); System.out.println(person5.compareTo(person4)); } /** * 实现toString */ @Test public void toStringTest() { System.out.println(Objects.toStringHelper(this).add("name", "shma").toString()); System.out.println(Objects.toStringHelper(Person.class).add("name", "shma").add("age", 23).toString()); Person person1 = new Person("jqq", 24); Person person2 = new Person("jqq", 24); System.out.println(person1); System.out.println(person2); System.out.println(person1.hashCode()); System.out.println(person2.hashCode()); System.out.println(person1.equals(person2)); } /** * 判断equals */ @Test public void equalsTest() { System.out.println(Objects.equal(null, "a")); //false System.out.println(Objects.equal("a", "a")); //true System.out.println(Objects.equal("", "")); //true System.out.println(Objects.equal("a", "")); //false System.out.println(Objects.equal(null, null)); //true System.out.println(Objects.equal(new Person("shma", 23), new Person("shma", 23))); //false Person person = new Person("jqq", 24); System.out.println(Objects.equal(person, person)); //true } /** * 计算hashcode * */ @Test public void hashCodeTest() { System.out.println(Objects.hashCode("a")); //128 System.out.println(Objects.hashCode("a")); //128 System.out.println(Objects.hashCode("a", "b")); //4066 System.out.println(Objects.hashCode("b", "a")); //4096 System.out.println(Objects.hashCode("b", "a", "c")); //127075 System.out.println(Objects.hashCode("a", "c", "b")); //126175 System.out.println(Objects.hashCode(new Person("shma", 23))); //21648900 System.out.println(Objects.hashCode(new Person("shma", 23))); //21846074 Person person = new Person("jqq", 24); System.out.println(Objects.hashCode(person)); //13856786 System.out.println(Objects.hashCode(person)); //13856786 } class Person implements Comparable<Person> { public String name; public int age; Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return Objects.toStringHelper(Person.class) .add("name", this.name) .add("age", this.age) .toString(); } @Override public int hashCode() { return Objects.hashCode(name, age); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if(this.name == other.name && this.age == other.age) return true; return false; } @Override public int compareTo(Person perosn) { return ComparisonChain.start() .compare(this.name, perosn.name) .compare(this.age, perosn.age) .result(); } } class Student implements Comparable<Student> { private String name; private int age; private int score; public Student() { super(); } public Student(String name, int age, int score) { super(); this.name = name; this.age = age; this.score = score; } @Override public String toString() { return Objects.toStringHelper(this) .add("name", name) .add("age", age) .add("score", score) .toString(); } @Override public int hashCode() { return Objects.hashCode(name, age); } @Override public boolean equals(Object obj) { if (obj instanceof Student) { Student that = (Student) obj; return Objects.equal(name, that.name) && Objects.equal(age, that.age) && Objects.equal(score, that.score); } return false; } @Override public int compareTo(Student student) { return ComparisonChain.start() .compare(this.name, student.name) .compare(this.age, student.age) .compare(this.score, student.score) .result(); } } } |