- Google Guava API provides collections, caching, primitives support, concurrency libraries, common annotations, string processing.
- This library can be downloaded from :-
http://code.google.com/p/guava-libraries/
Project Structure:-
Test Data:-
- We will build some demo data to test the Google Guava API. We will take Student as Value object.
Student.java:-
package com.sandeep.google.guava;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int roll;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRoll() {
return roll;
}
public void setRoll(int roll) {
this.roll = roll;
}
@Override
public String toString() {
return "Student [name=" + name + ", roll=" + roll + "]";
}
}
StudentDemoDataService.java:-
package com.sandeep.google.guava;
import java.util.ArrayList;
import java.util.List;
public class StudentDemoDataService {
public static ListgetListOfStudents() {
ListlistOfStudent = new ArrayList ();
Student s1 = new Student();
s1.setName("Sandeep");
s1.setRoll(23);
Student s2 = new Student();
s2.setName("Surabhi");
s2.setRoll(33);
Student s3 = new Student();
s3.setName("Sumanta");
s3.setRoll(25);
Student s4 = new Student();
s4.setName("Sangeeta");
s4.setRoll(23);
Student s5 = null;
listOfStudent.add(s1);
listOfStudent.add(s2);
listOfStudent.add(s5);
listOfStudent.add(s3);
listOfStudent.add(s4);
return listOfStudent;
}
}
Guava Joiner & Splitter:-
- This feature of Guava provides adding and splitting of toString() methods of objects.
- It provides a controlled accessibility to NULL value and objects.
Testing Joiner & Splitter:-
- Concatenating Objects toString with # and splitting the String to sub strings.
package com.sandeep.google.guava;
import java.util.Iterator;
import java.util.List;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
public class GoogleGuavaCollectionDemo {
public static void main(String[] args) {
ListlistOfStudent = StudentDemoDataService
.getListOfStudents();
Joiner joiner = Joiner.on("#").skipNulls();
String joinedString = joiner.join(listOfStudent);
System.out.print("Joined String : " + joinedString + "n");
Splitter splitter = Splitter.on("#");
IterableitrString = splitter.split(joinedString);
Iteratoritr = itrString.iterator();
while (itr.hasNext()) {
String elementString = itr.next();
System.out.print("elementString : " + elementString + "n");
}
}
}
Output:-
Joined String : Student [name=Sandeep, roll=23]#Student [name=Surabhi, roll=33]#Student [name=Sumanta, roll=25]#Student [name=Sangeeta, roll=23]
elementString : Student [name=Sandeep, roll=23]
elementString : Student [name=Surabhi, roll=33]
elementString : Student [name=Sumanta, roll=25]
elementString : Student [name=Sangeeta, roll=23]
Guava Funnel and Filter:-
- Guava filters are based on Bloom Filter . This is based on a data Structure to rapidly and memory efficiently searched an element. It is based on bit vector.
package com.sandeep.google.guava;
import java.util.List;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnel;
import com.google.common.hash.PrimitiveSink;
public class GuavaFunnelDemo {
@SuppressWarnings("serial")
public static void main(String[] args) {
// Creating a Student Funnel
FunnelstudentFunnel = new Funnel () {
@Override
public void funnel(Student aStudent, PrimitiveSink intoStudentFunnel) {
intoStudentFunnel.putString(aStudent.getName()).putInt(
aStudent.getRoll());
}
};
// Student Filter for a funnel type Stusdent with expected insertion 10
BloomFilterstudentBloomFilter = BloomFilter.create(
studentFunnel, 10, 0.01);
ListlistOfStudent = StudentDemoDataService
.getListOfStudents();
for (Student aStudent : listOfStudent) {
if (aStudent != null) {
studentBloomFilter.put(aStudent);
}
}
Student anotherStudent = new Student();
anotherStudent.setName("Sandeep");
anotherStudent.setRoll(23);
boolean mightContain = studentBloomFilter.mightContain(anotherStudent);
System.out.print("Might Contain : " + mightContain + "n");
}
}
Output:-
Might Contain : true
Guava Optional:-
package com.sandeep.google.guava;
import com.google.common.base.Optional;
public class GuavaBasicUtilitiesDemo {
public static void main(String[] args) {
Student dummyStudent = new Student();
dummyStudent.setName("Dummy Name");
dummyStudent.setRoll(0);
Optionalopt = Optional.of(dummyStudent);
System.out.println(opt.isPresent());
}
}
output:-
true