Google Gauva library to work with the collection objects.
The Guava project contains several of Google's core libraries that we rely on in our Java-based projects: collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, and so forth. This blog post deals with the Collection utilities using Functional programming. As of Java 7, functional programming in Java can only be approximated through awkward and verbose use of anonymous classes. This is expected to change in Java 8, Please be aware that excessive use of Guava's functional programming idioms can lead to verbose, confusing, unreadable, and inefficient code. Hre is a sample code to demonstrate partitioning, transforming, and filtering.
Step 1: The Guava library dependency in pom.xml file.
<!-- Google GUAVA --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>13.0.1</version> </dependency>
Step 2: Sample code using the Gauva library.
package com.mycompany.app11; import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.collect.FluentIterable; import com.google.common.collect.Iterables; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class GuavaMain { public static void main(String[] args) { GuavaMain main = new GuavaMain(); List<Employee> initialData = main.prepareData(); //Split into batches Iterable<List<Employee>> batchedData = main.partitionData(initialData); System.out.println("Batching -- 2 batches of employees: " + batchedData); //transform the above batches of employees to extract their names Iterable<Set<String>> transformedData = main.transformData(batchedData); System.out.println("Transforming -- " + transformedData); //concat and filter with a predicate Set<String> filteredData = main.filterData(transformedData); System.out.println("Filtering -- " + filteredData); } private List<Employee> prepareData() { //create 4 employees Employee john = new Employee(22, "John"); Employee peter = new Employee(23, "Peter"); Employee sam = new Employee(24, "Sam"); Employee eric = new Employee(25, "Eric"); List<Employee> employees = new ArrayList<Employee>(); employees.add(john); employees.add(peter); employees.add(sam); employees.add(eric); return employees; } /** * Say we want to process in batches of 2 employees */ public Iterable<List<Employee>> partitionData(List<Employee> departments) { //using Gauva library's partition method return Iterables.partition(departments, 2);// batches of 2 employees. } /** * Say we want to transform batched employees to extract their names */ public Iterable<Set<String>> transformData(Iterable<List<Employee>> batchedData) { //define an anonymous function Function<List<Employee>, Set<String>> extractNamesFunction = new Function<List<Employee>, Set<String>>() { public Set<String> apply(List<Employee> batch) { final Set<String> names = new HashSet<String>(batch.size()); for (Employee employee : batch) { names.add(employee.getName()); } return names; } }; //using Gauva library's transform method Iterable<Set<String>> transformedData = Iterables.transform(batchedData, extractNamesFunction); return transformedData; } /** * Say we want to filter data so that names shorter than 3 characters are * filtered out */ public Set<String> filterData(Iterable<Set<String>> transformedData) { //using Gauva library's concat method Iterable<String> concatenatedData = Iterables.concat(transformedData); //Define a predicate for filtering data as an anonymous function Predicate<String> longerThanThreeCharacters = new Predicate<String>() { public boolean apply(String input) { return input.length() > 3; } }; //filter FluentIterable<String> filteredData = FluentIterable.from(concatenatedData).filter(longerThanThreeCharacters); return filteredData.toImmutableSet(); } }
package com.mycompany.app11; public class Employee { private Integer id; private String name; public Employee(Integer id, String name) { super(); this.id = id; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + "]"; } }
The Employee POJO:
Step 3: Finally, the output:
Batching -- 2 batches of employees: [[Person [id=22, name=John], Person [id=23, name=Peter]], [Person [id=24, name=Sam], Person [id=25, name=Eric]]] Transforming -- [[Peter, John], [Sam, Eric]] Filtering -- [Peter, John, Eric]
Reference: Guava Collection Utilities
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home