Guava下载地址:http://pan.baidu.com/s/1o8rQmds(百度云)
http://search.maven.org/remotecontent?filepath=com/google/guava/guava/18.0/guava-18.0.jar(官网)
相关教程:
英文:http://www.tutorialspoint.com/guava/guava_caching_utilities.htm
中文:http://ifeve.com/google-guava/
Caching Utilities
package test; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import com.google.common.base.MoreObjects; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; public class GuavaTester { public static void main(String args[]) { //create a cache for employees based on their employee id LoadingCache<String, Employee> employeeCache = CacheBuilder.newBuilder() .maximumSize(100) // maximum 100 records can be cached .expireAfterAccess(30, TimeUnit.MINUTES) // cache will expire after 30 minutes of access .build(new CacheLoader<String, Employee>(){ // build the cacheloader @Override public Employee load(String empId) throws Exception { //make the expensive call return getFromDatabase(empId); } }); try { //on first invocation, cache will be populated with corresponding //employee record System.out.println("Invocation #1"); System.out.println(employeeCache.get("100")); System.out.println(employeeCache.get("103")); System.out.println(employeeCache.get("110")); //second invocation, data will be returned from cache System.out.println("Invocation #2"); System.out.println(employeeCache.get("100")); System.out.println(employeeCache.get("103")); System.out.println(employeeCache.get("110")); }catch (ExecutionException e) { e.printStackTrace(); } } private static Employee getFromDatabase(String empId) { Employee e1 = new Employee("Mahesh", "Finance", "100"); Employee e2 = new Employee("Rohan", "IT", "103"); Employee e3 = new Employee("Sohan", "Admin", "110"); Map<String, Employee> database = new HashMap<String, Employee>(); database.put("100", e1); database.put("103", e2); database.put("110", e3); System.out.println("Database hit for" + empId); return database.get(empId); } } class Employee { String name; String dept; String emplD; public Employee(String name, String dept, String empID) { this.name = name; this.dept = dept; this.emplD = empID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } public String getEmplD() { return emplD; } public void setEmplD(String emplD) { this.emplD = emplD; } @Override public String toString() { return MoreObjects.toStringHelper(Employee.class) .add("Name", name) .add("Department", dept) .add("Emp Id", emplD).toString(); } }
输出:
Invocation #1 Database hit for100 Employee{Name=Mahesh, Department=Finance, Emp Id=100} Database hit for103 Employee{Name=Rohan, Department=IT, Emp Id=103} Database hit for110 Employee{Name=Sohan, Department=Admin, Emp Id=110} Invocation #2 Employee{Name=Mahesh, Department=Finance, Emp Id=100} Employee{Name=Rohan, Department=IT, Emp Id=103} Employee{Name=Sohan, Department=Admin, Emp Id=110}
OptionalClass
package test; import com.google.common.base.Optional; public class OptionalClass { public static void main(String args[]) { OptionalClass OptionalClass = new OptionalClass(); Integer value1 = null; Integer value2 = new Integer(10); //Optional.fromNullable - allows passed parameter to be null. Optional<Integer> a = Optional.fromNullable(value1); //Optional.of - throws NullPointerException if passed parameter is null Optional<Integer> b = Optional.of(value2); System.out.println(OptionalClass.sum(a,b)); } public Integer sum(Optional<Integer> a, Optional<Integer> b) { //Optional.isPresent - checks the value is present or not System.out.println("First parameter is present: " + a.isPresent()); System.out.println("Second parameter is present: " + b.isPresent()); //Optional.or - returns the value if present otherwise returns //the default value passed. Integer value1 = a.or(new Integer(0)); //Optional.get - gets the value, value should be present Integer value2 = b.get(); return value1 + value2; } }
输出:
First parameter is present: false Second parameter is present: true 10
PreconditionsTest
package test; import com.google.common.base.Preconditions; public class PreconditionsTest { public static void main(String args[]) { PreconditionsTest PreconditionsTest = new PreconditionsTest(); try { System.out.println(PreconditionsTest.sqrt(-3.0)); }catch(IllegalArgumentException e) { System.out.println(e.getMessage()); } try { System.out.println(PreconditionsTest.sum(null,3)); }catch(NullPointerException e) { System.out.println(e.getMessage()); } try { System.out.println(PreconditionsTest.getValue(6)); }catch(IndexOutOfBoundsException e) { System.out.println(e.getMessage()); } } public double sqrt(double input) throws IllegalArgumentException { Preconditions.checkArgument(input > 0.0, "Illegal Argument passed: Negative value %s.", input); return Math.sqrt(input); } public int sum(Integer a, Integer b){ a = Preconditions.checkNotNull(a, "Illegal Argument passed: First parameter is Null."); b = Preconditions.checkNotNull(b, "Illegal Argument passed: Second parameter is Null."); return a+b; } public int getValue(int input){ int[] data = {1,2,3,4,5}; Preconditions.checkElementIndex(input,data.length, "Illegal Argument passed: Invalid index."); return 0; } }
输出:
Illegal Argument passed: Negative value -3.0.
Illegal Argument passed: First parameter is Null.
Illegal Argument passed: Invalid index. (6) must be less than size (5)
ObjectsClass
package test; import com.google.common.base.Objects; public class ObjectsClass { public static void main(String args[]) { Student s1 = new Student("Mahesh", "Parashar", 1, "VI"); Student s2 = new Student("Suresh", null, 3, null); System.out.println(s1.equals(s2)); System.out.println(s1.hashCode()); System.out.println( Objects.toStringHelper(s1) .add("Name",s1.getFirstName()+" " + s1.getLastName()) .add("Class", s1.getClassName()) .add("Roll No", s1.getRollNo()) .toString()); } } class Student { private String firstName; private String lastName; private int rollNo; private String className; public Student(String firstName, String lastName, int rollNo, String className) { this.firstName = firstName; this.lastName = lastName; this.rollNo = rollNo; this.className = className; } @Override public boolean equals(Object object) { if(!(object instanceof Student) || object == null){ return false; } Student student = (Student)object; // no need to handle null here // Objects.equal("test", "test") == true // Objects.equal("test", null) == false // Objects.equal(null, "test") == false // Objects.equal(null, null) == true return Objects.equal(firstName, student.firstName) // first name can be null && Objects.equal(lastName, student.lastName) // last name can be null && Objects.equal(rollNo, student.rollNo) && Objects.equal(className, student.className);// class name can be null } @Override public int hashCode() { //no need to compute hashCode by self return Objects.hashCode(className,rollNo); } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } }
输出:
false 85871 Student{Name=Mahesh Parashar, Class=VI, Roll No=1}
OrderingClass
package test; import java.util.ArrayList; import java.util.Collections; import java.util.List; import com.google.common.collect.Ordering; public class OrderingClass { public static void main(String args[]) { List<Integer> numbers = new ArrayList<Integer>(); numbers.add(new Integer(5)); numbers.add(new Integer(2)); numbers.add(new Integer(15)); numbers.add(new Integer(51)); numbers.add(new Integer(53)); numbers.add(new Integer(35)); numbers.add(new Integer(45)); numbers.add(new Integer(32)); numbers.add(new Integer(43)); numbers.add(new Integer(16)); Ordering ordering = Ordering.natural(); System.out.println("Input List: "); System.out.println(numbers); Collections.sort(numbers,ordering ); System.out.println("Sorted List: "); System.out.println(numbers); System.out.println("======================"); System.out.println("List is sorted: " + ordering.isOrdered(numbers)); System.out.println("Minimum: " + ordering.min(numbers)); System.out.println("Maximum: " + ordering.max(numbers)); Collections.sort(numbers,ordering.reverse()); System.out.println("Reverse: " + numbers); numbers.add(null); System.out.println("Null added to Sorted List: "); System.out.println(numbers); Collections.sort(numbers,ordering.nullsFirst()); System.out.println("Null first Sorted List: "); System.out.println(numbers); System.out.println("======================"); List<String> names = new ArrayList<String>(); names.add("Ram"); names.add("Shyam"); names.add("Mohan"); names.add("Sohan"); names.add("Ramesh"); names.add("Suresh"); names.add("Naresh"); names.add("Mahesh"); names.add(null); names.add("Vikas"); names.add("Deepak"); System.out.println("Another List: "); System.out.println(names); Collections.sort(names,ordering.nullsFirst().reverse()); System.out.println("Null first then reverse sorted list: "); System.out.println(names); } }
输出:
Input List: [5, 2, 15, 51, 53, 35, 45, 32, 43, 16] Sorted List: [2, 5, 15, 16, 32, 35, 43, 45, 51, 53] ====================== List is sorted: true Minimum: 2 Maximum: 53 Reverse: [53, 51, 45, 43, 35, 32, 16, 15, 5, 2] Null added to Sorted List: [53, 51, 45, 43, 35, 32, 16, 15, 5, 2, null] Null first Sorted List: [null, 2, 5, 15, 16, 32, 35, 43, 45, 51, 53] ====================== Another List: [Ram, Shyam, Mohan, Sohan, Ramesh, Suresh, Naresh, Mahesh, null, Vikas, Deepak] Null first then reverse sorted list: [Vikas, Suresh, Sohan, Shyam, Ramesh, Ram, Naresh, Mohan, Mahesh, Deepak, null]
RangeClass
package test; import com.google.common.collect.ContiguousSet; import com.google.common.collect.DiscreteDomain; import com.google.common.collect.Range; import com.google.common.primitives.Ints; public class RangeClass { public static void main(String args[]) { RangeClass tester = new RangeClass(); tester.testRange(); } private void testRange() { //create a range [a,b] = { x | a <= x <= b} Range<Integer> range1 = Range.closed(0, 9); System.out.print("[0,9] : "); printRange(range1); System.out.println("5 is present: " + range1.contains(5)); System.out.println("(1,2,3) is present: " + range1.containsAll(Ints.asList(1, 2, 3))); System.out.println("Lower Bound: " + range1.lowerEndpoint()); System.out.println("Upper Bound: " + range1.upperEndpoint()); //create a range (a,b) = { x | a < x < b} Range<Integer> range2 = Range.open(0, 9); System.out.print("(0,9) : "); printRange(range2); //create a range (a,b] = { x | a < x <= b} Range<Integer> range3 = Range.openClosed(0, 9); System.out.print("(0,9] : "); printRange(range3); //create a range [a,b) = { x | a <= x < b} Range<Integer> range4 = Range.closedOpen(0, 9); System.out.print("[0,9) : "); printRange(range4); //create an open ended range (9, infinity Range<Integer> range5 = Range.greaterThan(9); System.out.println("(9,infinity) : "); System.out.println("Lower Bound: " + range5.lowerEndpoint()); System.out.println("Upper Bound present: " + range5.hasUpperBound()); Range<Integer> range6 = Range.closed(3, 5); printRange(range6); //check a subrange [3,5] in [0,9] System.out.println("[0,9] encloses [3,5]:" + range1.encloses(range6)); Range<Integer> range7 = Range.closed(9, 20); printRange(range7); //check ranges to be connected System.out.println("[0,9] is connected [9,20]:" + range1.isConnected(range7)); Range<Integer> range8 = Range.closed(5, 15); //intersection printRange(range1.intersection(range8)); //span printRange(range1.span(range8)); } private void printRange(Range<Integer> range) { System.out.print("[ "); for(int grade : ContiguousSet.create(range, DiscreteDomain.integers())) { System.out.print(grade +" "); } System.out.println("]"); } }
输出:
[0,9] : [ 0 1 2 3 4 5 6 7 8 9 ] 5 is present: true (1,2,3) is present: true Lower Bound: 0 Upper Bound: 9 (0,9) : [ 1 2 3 4 5 6 7 8 ] (0,9] : [ 1 2 3 4 5 6 7 8 9 ] [0,9) : [ 0 1 2 3 4 5 6 7 8 ] (9,infinity) : Lower Bound: 9 Upper Bound present: false [ 3 4 5 ] [0,9] encloses [3,5]:true [ 9 10 11 12 13 14 15 16 17 18 19 20 ] [0,9] is connected [9,20]:true [ 5 6 7 8 9 ] [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ]
JoinerClass
package test; import java.util.Arrays; import com.google.common.base.Joiner; public class JoinerClass { public static void main(String args[]){ JoinerClass tester = new JoinerClass(); tester.testJoiner(); } private void testJoiner(){ System.out.println(Joiner.on(",") .skipNulls() .join(Arrays.asList(1,2,3,4,5,null,6))); } }
输出:
1,2,3,4,5,6
SplitterClass
package test; import com.google.common.base.Splitter; public class SplitterClass { public static void main(String args[]) { SplitterClass tester = new SplitterClass(); tester.testSplitter(); } private void testSplitter() { System.out.println(Splitter.on(',') .trimResults() .omitEmptyStrings() .split("the ,quick, ,brown, fox, jumps, over, the, lazy, little dog.")); } }
输出:
[the, quick, brown, fox, jumps, over, the, lazy, little dog.]
CharMatcherClass
package test; import com.google.common.base.CharMatcher; public class CharMatcherClass { public static void main(String args[]) { CharMatcherClass tester = new CharMatcherClass(); tester.testCharMatcher(); } private void testCharMatcher() { System.out.println(CharMatcher.DIGIT.retainFrom("mahesh123")); // only the digits System.out.println(CharMatcher.WHITESPACE.trimAndCollapseFrom(" Mahesh Parashar ", ' ')); // trim whitespace at ends, and replace/collapse whitespace into single spaces System.out.println(CharMatcher.JAVA_DIGIT.replaceFrom("mahesh123", "*")); // star out all digits System.out.println(CharMatcher.JAVA_DIGIT.or(CharMatcher.JAVA_UPPER_CASE).retainFrom("Umahesh123")); // eliminate all characters that aren't digits or lowercase } }
输出:
123 Mahesh Parashar mahesh*** U123
BytesClass
package test; import java.util.List; import com.google.common.primitives.Bytes; public class BytesClass { public static void main(String args[]){ BytesClass tester = new BytesClass(); tester.testBytes(); } private void testBytes() { byte[] byteArray = {1,2,3,4,5,5,7,9,9}; //convert array of primitives to array of objects List<Byte> objectArray = Bytes.asList(byteArray); System.out.println(objectArray.toString()); //convert array of objects to array of primitives byteArray = Bytes.toArray(objectArray); System.out.print("[ "); for(int i = 0; i< byteArray.length ; i++){ System.out.print(byteArray[i] + " "); } System.out.println("]"); byte data = 5; //check if element is present in the list of primitives or not System.out.println("5 is in list? " + Bytes.contains(byteArray, data)); //Returns the index System.out.println("Index of 5: " + Bytes.indexOf(byteArray,data)); //Returns the last index maximum System.out.println("Last index of 5: " + Bytes.lastIndexOf(byteArray,data)); } }
输出:
[1, 2, 3, 4, 5, 5, 7, 9, 9] [ 1 2 3 4 5 5 7 9 9 ] 5 is in list? true Index of 5: 4 Last index of 5: 5
8种舍入方式:
1、ROUND_UP 舍入远离零的舍入模式。 在丢弃非零部分之前始终增加数字(始终对非零舍弃部分前面的数字加1)。 注意,此舍入模式始终不会减少计算值的大小。 2、ROUND_DOWN 接近零的舍入模式。 在丢弃某部分之前始终不增加数字(从不对舍弃部分前面的数字加1,即截短)。 注意,此舍入模式始终不会增加计算值的大小。 3、ROUND_CEILING 接近正无穷大的舍入模式。 如果 BigDecimal 为正,则舍入行为与 ROUND_UP 相同; 如果为负,则舍入行为与 ROUND_DOWN 相同。 注意,此舍入模式始终不会减少计算值。 4、ROUND_FLOOR 接近负无穷大的舍入模式。 如果 BigDecimal 为正,则舍入行为与 ROUND_DOWN 相同; 如果为负,则舍入行为与 ROUND_UP 相同。 注意,此舍入模式始终不会增加计算值。 5、ROUND_HALF_UP 向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则为向上舍入的舍入模式。 如果舍弃部分 >= 0.5,则舍入行为与 ROUND_UP 相同;否则舍入行为与 ROUND_DOWN 相同。 注意,这是我们大多数人在小学时就学过的舍入模式(四舍五入)。 6、ROUND_HALF_DOWN 向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则为上舍入的舍入模式。 如果舍弃部分 > 0.5,则舍入行为与 ROUND_UP 相同;否则舍入行为与 ROUND_DOWN 相同(五舍六入)。 7、ROUND_HALF_EVEN 向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则向相邻的偶数舍入。 如果舍弃部分左边的数字为奇数,则舍入行为与 ROUND_HALF_UP 相同; 如果为偶数,则舍入行为与 ROUND_HALF_DOWN 相同。 注意,在重复进行一系列计算时,此舍入模式可以将累加错误减到最小。 此舍入模式也称为“银行家舍入法”,主要在美国使用。四舍六入,五分两种情况。 如果前一位为奇数,则入位,否则舍去。 以下例子为保留小数点1位,那么这种舍入方式下的结果。 1.15>1.2 1.25>1.2 8、ROUND_UNNECESSARY 断言请求的操作具有精确的结果,因此不需要舍入。 如果对获得精确结果的操作指定此舍入模式,则抛出ArithmeticException。
MultisetClass
package test; import java.util.Iterator; import java.util.Set; import com.google.common.collect.HashMultiset; import com.google.common.collect.Multiset; public class MultisetClass { public static void main(String args[]) { //create a multiset collection Multiset<String> multiset = HashMultiset.create(); multiset.add("a"); multiset.add("b"); multiset.add("c"); multiset.add("d"); multiset.add("a"); multiset.add("b"); multiset.add("c"); multiset.add("b"); multiset.add("b"); multiset.add("b"); //print the occurrence of an element System.out.println("Occurrence of 'b' : "+multiset.count("b")); //print the total size of the multiset System.out.println("Total Size : "+multiset.size()); //get the distinct elements of the multiset as set Set<String> set = multiset.elementSet(); //display the elements of the set System.out.print("Set ["); for (String s : set) { System.out.print(s+" "); } System.out.println("]"); //display all the elements of the multiset using iterator Iterator<String> iterator = multiset.iterator(); System.out.print("MultiSet ["); while(iterator.hasNext()){ System.out.print(iterator.next()+" "); } System.out.println("]"); //display the distinct elements of the multiset with their occurrence count System.out.println("MultiSet ["); for (Multiset.Entry<String> entry : multiset.entrySet()) { System.out.println("Element: " + entry.getElement() + ", Occurrence(s): " + entry.getCount()); } System.out.println("]"); //remove extra occurrences multiset.remove("b",2); //print the occurrence of an element System.out.println("Occurence of 'b' : " + multiset.count("b")); } }
输出:
Occurrence of 'b' : 5 Total Size : 10 Set [a b c d ] MultiSet [a a b b b b b c c d ] MultiSet [ Element: a, Occurrence(s): 2 Element: b, Occurrence(s): 5 Element: c, Occurrence(s): 2 Element: d, Occurrence(s): 1 ] Occurence of 'b' : 3
MultimapTest
package test; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap; public class MultimapTest { public static void main(String args[]) { MultimapTest tester = new MultimapTest(); Multimap<String,String> multimap = tester.getMultimap(); List<String> lowerList = (List<String>)multimap.get("lower"); System.out.println("Initial lower case list"); System.out.println(lowerList.toString()); lowerList.add("f"); System.out.println("Modified lower case list"); System.out.println(lowerList.toString()); List<String> upperList = (List<String>)multimap.get("upper"); System.out.println("Initial upper case list"); System.out.println(upperList.toString()); upperList.remove("D"); System.out.println("Modified upper case list"); System.out.println(upperList.toString()); Map<String, Collection<String>> map = multimap.asMap(); System.out.println("Multimap as a map"); for (Map.Entry<String, Collection<String>> entry : map.entrySet()) { String key = entry.getKey(); Collection<String> value = multimap.get(key); System.out.println(key + ":" + value); } System.out.println("Keys of Multimap"); Set<String> keys = multimap.keySet(); for(String key:keys){ System.out.println(key); } System.out.println("Values of Multimap"); Collection<String> values = multimap.values(); System.out.println(values); } private Multimap<String,String> getMultimap(){ //Map<String, List<String>> // lower -> a, b, c, d, e // upper -> A, B, C, D Multimap<String,String> multimap = ArrayListMultimap.create(); multimap.put("lower", "a"); multimap.put("lower", "b"); multimap.put("lower", "c"); multimap.put("lower", "d"); multimap.put("lower", "e"); multimap.put("upper", "A"); multimap.put("upper", "B"); multimap.put("upper", "C"); multimap.put("upper", "D"); return multimap; } }
输出:
Initial lower case list [a, b, c, d, e] Modified lower case list [a, b, c, d, e, f] Initial upper case list [A, B, C, D] Modified upper case list [A, B, C] Multimap as a map lower:[a, b, c, d, e, f] upper:[A, B, C] Keys of Multimap lower upper Values of Multimap [a, b, c, d, e, f, A, B, C]
TableInterface
package test; import java.util.Map; import java.util.Set; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; public class TableInterface { public static void main(String args[]) { //Table<R,C,V> == Map<R,Map<C,V>> /* * Company: IBM, Microsoft, TCS * IBM -> {101:Mahesh, 102:Ramesh, 103:Suresh} * Microsoft -> {101:Sohan, 102:Mohan, 103:Rohan } * TCS -> {101:Ram, 102: Shyam, 103: Sunil } * * */ //create a table Table<String, String, String> employeeTable = HashBasedTable.create(); //initialize the table with employee details employeeTable.put("IBM", "101","Mahesh"); employeeTable.put("IBM", "102","Ramesh"); employeeTable.put("IBM", "103","Suresh"); employeeTable.put("Microsoft", "111","Sohan"); employeeTable.put("Microsoft", "112","Mohan"); employeeTable.put("Microsoft", "113","Rohan"); employeeTable.put("TCS", "121","Ram"); employeeTable.put("TCS", "122","Shyam"); employeeTable.put("TCS", "123","Sunil"); //get Map corresponding to IBM Map<String,String> ibmEmployees = employeeTable.row("IBM"); System.out.println("List of IBM Employees"); for(Map.Entry<String, String> entry : ibmEmployees.entrySet()){ System.out.println("Emp Id: " + entry.getKey() + ", Name: " + entry.getValue()); } //get all the unique keys of the table Set<String> employers = employeeTable.rowKeySet(); System.out.print("Employers: "); for(String employer: employers){ System.out.print(employer + " "); } System.out.println(); //get a Map corresponding to 102 Map<String,String> EmployerMap = employeeTable.column("102"); for(Map.Entry<String, String> entry : EmployerMap.entrySet()){ System.out.println("Employer: " + entry.getKey() + ", Name: " + entry.getValue()); } } }
输出:
List of IBM Employees Emp Id: 103, Name: Suresh Emp Id: 101, Name: Mahesh Emp Id: 102, Name: Ramesh Employers: IBM TCS Microsoft Employer: IBM, Name: Ramesh
BiMapTest
package test; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; public class BiMapTest { public static void main(String args[]) { BiMap<Integer, String> empIDNameMap = HashBiMap.create(); empIDNameMap.put(new Integer(101), "Mahesh"); empIDNameMap.put(new Integer(102), "Sohan"); empIDNameMap.put(new Integer(103), "Ramesh"); //Emp Id of Employee "Mahesh" System.out.println(empIDNameMap.inverse().get("Mahesh")); } }
输出:
101