Guava Function Example

0

Guava Function<I, O> is a simple interface with method called apply(input) that takes in an input and returns an output. If you are using Java 8, you don’t need Guava Function interface a Java 8 supports functional programming, see Java 8 Function.

In this article, we will see an example of Function and then an example of Functions which provides some utility methods which internally implements Function.

Function Example

In our first example, we have a list which contains group of words, we will use Collections2.transform() to apply a custom function on each list element. The custom function is applied on each list element splits the phrase into individual words.

FunctionExample:

package com.javarticles.guava;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;

public class FunctionExample {
    public static void main(String[] args) {
        List inputList = Arrays.asList("Hi There", "I am", "testing functions");
        System.out.println(inputList);
        Function<String, String[]> splitByWords = new Function<String, String[]>(){

            public String[] apply(String input) {
                return input.split(" ");
            }};
            
        Collection<String[]> wordsList = Collections2.transform(inputList, splitByWords);
        List allWords = new ArrayList();
        for (String[] words : wordsList) {
            allWords.addAll(Arrays.asList(words));
        }
        System.out.println(allWords);
    }
}

Output:

[Hi There, I am, testing functions]
[Hi, There, I, am, testing, functions]

Functions Example

In our next example, we will apply the Guava provided Functions.toStringFunction which converts each list element to its string representation using Object’s toString().

FunctionsToString:

package com.javarticles.guava;

import java.util.Arrays;
import java.util.List;

import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.collect.Collections2;

public class FunctionsToString {
    public static void main(String[] args) {
        Function<Object, String> toString = Functions.toStringFunction();
        List empList = Arrays.asList(emp("1", "Joe"), emp("2", "Sam"), emp("3", "Rahul"));
        System.out.println(Collections2.transform(empList, toString));
    }
    
    private static Employee emp(String id, String name) {
        return new Employee(id, name);
    }
    
    private static class Employee {
        private String id;
        private String name;
        Employee(String id, String name) {
            this.id = id;
            this.name = name;
        }
        public String toString() {
            return "Emp<" + id + "," + name + ">";
        }
    }
}

Output:

[Emp<1,Joe>, Emp<2,Sam>, Emp<3,Rahul>]

Download the source code

This was an example about Guava Functions.

You can download the source code here: guavaFunctionsExamples.zip
Share.

Comments are closed.