1. Overview

This cookbook is organized into small and focused recipes and code snippets for using the Guava functional-style elements – Predicates and Functions.

The cookbook format is focused and practical – no extraneous details and explanations necessary.

2. The Cookbook

filter a collection by a condition (custom Predicate)

List<Integer> numbers = Lists.newArrayList(1, 2, 3, 6, 10, 34, 57, 89); Predicate<Integer> acceptEven = new Predicate<Integer>() { @Override public boolean apply(Integer number) { return (number % 2) == 0; } }; List<Integer> evenNumbers = Lists.newArrayList(Collections2.filter(numbers, acceptEven)); Integer found = Collections.binarySearch(evenNumbers, 57); assertThat(found, lessThan(0));

filter out nulls from a collection

List<String> withNulls = Lists.newArrayList("a", "bc", null, "def"); Iterable<String> withoutNuls = Iterables.filter(withNulls, Predicates.notNull()); assertTrue(Iterables.all(withoutNuls, Predicates.notNull()));

check condition for all elements of a collection

List<Integer> evenNumbers = Lists.newArrayList(2, 6, 8, 10, 34, 90); Predicate<Integer> acceptEven = new Predicate<Integer>() { @Override public boolean apply(Integer number) { return (number % 2) == 0; } }; assertTrue(Iterables.all(evenNumbers, acceptEven));

negate a predicate

List<Integer> evenNumbers = Lists.newArrayList(2, 6, 8, 10, 34, 90); Predicate<Integer> acceptOdd = new Predicate<Integer>() { @Override public boolean apply(Integer number) { return (number % 2) != 0; } }; assertTrue(Iterables.all(evenNumbers, Predicates.not(acceptOdd)));

apply a simple function

List<Integer> numbers = Lists.newArrayList(1, 2, 3); List<String> asStrings = Lists.transform(numbers, Functions.toStringFunction()); assertThat(asStrings, contains("1", "2", "3"));

sort collection by first applying an intermediary function

List<Integer> numbers = Arrays.asList(2, 1, 11, 100, 8, 14); Ordering<Object> ordering = Ordering.natural().onResultOf(Functions.toStringFunction()); List<Integer> inAlphabeticalOrder = ordering.sortedCopy(numbers); List<Integer> correctAlphabeticalOrder = Lists.newArrayList(1, 100, 11, 14, 2, 8); assertThat(correctAlphabeticalOrder, equalTo(inAlphabeticalOrder));

complex example – chaining predicates and functions

List<Integer> numbers = Arrays.asList(2, 1, 11, 100, 8, 14); Predicate<Integer> acceptEvenNumber = new Predicate<Integer>() { @Override public boolean apply(Integer number) { return (number % 2) == 0; } }; Function<Integer, Integer> powerOfTwo = new Function<Integer, Integer>() { @Override public Integer apply(Integer input) { return (int) Math.pow(input, 2); } }; FluentIterable<Integer> powerOfTwoOnlyForEvenNumbers = FluentIterable.from(numbers).filter(acceptEvenNumber).transform(powerOfTwo); assertThat(powerOfTwoOnlyForEvenNumbers, contains(4, 10000, 64, 196));

compose two functions

List<Integer> numbers = Arrays.asList(2, 3); Function<Integer, Integer> powerOfTwo = new Function<Integer, Integer>() { @Override public Integer apply(Integer input) { return (int) Math.pow(input, 2); } }; List<Integer> result = Lists.transform(numbers, Functions.compose(powerOfTwo, powerOfTwo)); assertThat(result, contains(16, 81));

create a Map backed by a Set and a Function

Function<Integer, Integer> powerOfTwo = new Function<Integer, Integer>() { @Override public Integer apply(Integer input) { return (int) Math.pow(input, 2); } }; Set<Integer> lowNumbers = Sets.newHashSet(2, 3, 4); Map<Integer, Integer> numberToPowerOfTwoMuttable = Maps.asMap(lowNumbers, powerOfTwo); Map<Integer, Integer> numberToPowerOfTwoImuttable = Maps.toMap(lowNumbers, powerOfTwo); assertThat(numberToPowerOfTwoMuttable.get(2), equalTo(4)); assertThat(numberToPowerOfTwoImuttable.get(2), equalTo(4));

create a Function out of a Predicate

List<Integer> numbers = Lists.newArrayList(1, 2, 3, 6); Predicate<Integer> acceptEvenNumber = new Predicate<Integer>() { @Override public boolean apply(Integer number) { return (number % 2) == 0; } }; Function<Integer, Boolean> isEventNumberFunction = Functions.forPredicate(acceptEvenNumber); List<Boolean> areNumbersEven = Lists.transform(numbers, isEventNumberFunction); assertThat(areNumbersEven, contains(false, true, false, true));

3. More Guava Cookbooks

Guava is a comprehensive and fantastically useful library – here's a few more APIs covered in cookbook form:

Enjoy.

4. Conclusion

This format is a little different than my usual tutorials – mainly because this is an internal development cookbook that I have been keeping and using for quite some time. The goal is to have this information readily available online – and to add to it whenever I run into a new useful example.

The implementation of all these examples and code snippets can be found over on GitHub – this is a Maven-based project, so it should be easy to import and run as it is.