Guava Collections Cookbook

1. Introduction

This cookbook article is organized into small and focused recipes and code snippets for using Guava style collections.

The format is that of a growing list of code examples with no additional explanation necessary – it is meant to keep common usages of the API easy to access during development.

2. The Recipes

downcast a List<Parent> to a List<Child>

Note: this is a workaround for non-covariant generified collections in Java

class CastFunction<F, T extends F> implements Function<F, T> {
    @Override
    public final T apply(final F from) {
        return (T) from;
    }
}
List<TypeParent> originalList = Lists.newArrayList();
List<TypeChild> theList = Lists.transform(originalList, 
    new CastFunction<TypeParent, TypeChild>());

Simpler alternative without Guava – involving 2 cast operations

List<Number> originalList = Lists.newArrayList();
List<Integer> theList = (List<Integer>) (List<? extends Number>) originalList;

Adding an iterable to a collection

Iterable<String> iter = Lists.newArrayList();
Collection<String> collector = Lists.newArrayList();
Iterables.addAll(collector, iter);

Check if collection contains element(s) according to a custom matching rule

Iterable<String> theCollection = Lists.newArrayList("a", "bc", "def");
    boolean contains = Iterables.any(theCollection, new Predicate<String>() {
    @Override
    public boolean apply(final String input) {
        return input.length() == 1;
    }
});
assertTrue(contains);

Alternative solution using search

Iterable<String> theCollection = Sets.newHashSet("a", "bc", "def");
boolean contains = Iterables.find(theCollection, new Predicate<String>() {
    @Override
    public boolean apply(final String input) {
       return input.length() == 1;
    }
}) != null;
assertTrue(contains);

Alternative solution only applicable to Sets

Set<String> theCollection = Sets.newHashSet("a", "bc", "def");
boolean contains = !Sets.filter(theCollection, new Predicate<String>() {
    @Override
    public boolean apply(final String input) {
        return input.length() == 1;
    }
}).isEmpty();
assertTrue(contains);

NoSuchElementException on Iterables.find when nothing is found

Iterable<String> theCollection = Sets.newHashSet("abcd", "efgh", "ijkl");
Predicate<String> inputOfLengthOne = new Predicate<String>() {
    @Override
    public boolean apply(final String input) {
        return input.length() == 1;
    }
};
String found = Iterables.find(theCollection, inputOfLengthOne);

This will throw the NoSuchElementException exception:

java.util.NoSuchElementException
	at com.google.common.collect.AbstractIterator.next(AbstractIterator.java:154)
	at com.google.common.collect.Iterators.find(Iterators.java:712)
	at com.google.common.collect.Iterables.find(Iterables.java:643)

Solution: there is an overloaded find method that takes the default return value as an argument and can be called with null for the desired behavior:

String found = Iterables.find(theCollection, inputOfLengthOne, null);

Remove all null values from a collection

List<String> values = Lists.newArrayList("a", null, "b", "c");
Iterable<String> withoutNulls = Iterables.filter(values, Predicates.notNull());

Create immutable List/Set/Map directly

ImmutableList<String> immutableList = ImmutableList.of("a", "b", "c");
ImmutableSet<String> immutableSet = ImmutableSet.of("a", "b", "c");
ImmutableMap<String, String> imuttableMap = 
    ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3");

Create immutable List/Set/Map from a standard collection

List<String> muttableList = Lists.newArrayList();
ImmutableList<String> immutableList = ImmutableList.copyOf(muttableList);

Set<String> muttableSet = Sets.newHashSet();
ImmutableSet<String> immutableSet = ImmutableSet.copyOf(muttableSet);

Map<String, String> muttableMap = Maps.newHashMap();
ImmutableMap<String, String> imuttableMap = ImmutableMap.copyOf(muttableMap);

Alternative solution using builders

List<String> muttableList = Lists.newArrayList();
ImmutableList<String> immutableList = 
    ImmutableList.<String> builder().addAll(muttableList).build();

Set<String> muttableSet = Sets.newHashSet();
ImmutableSet<String> immutableSet = 
    ImmutableSet.<String> builder().addAll(muttableSet).build();

Map<String, String> muttableMap = Maps.newHashMap();
ImmutableMap<String, String> imuttableMap = 
    ImmutableMap.<String, String> builder().putAll(muttableMap).build();

3. More Guava Cookbooks

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

  • Guava Ordering Cookbook
  • Guava Functional Cookbook

Enjoy.

4. Going Forward

As I mentioned in the beginning, I am experimenting with this different format – the cookbook – to try to gather simple common tasks of using Guava Collections in a single place. The focus of this format is simplicity and speed, so most recipes have no additional explanation other than the code example itself.

Finally – I am looking at this as a living document – I’m going to keep adding recipes and examples as I run into them. Feel free to provide more in the comments and I’ll look to incorporate them into the cookbook.

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.

Related posts:

The Difference Between map() and flatMap()
Introduction to Spring Boot CLI
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Java Program to Implement Borwein Algorithm
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Check If a File or Directory Exists in Java
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Disable DNS caching
Getting Started with Stream Processing with Spring Cloud Data Flow
Spring MVC Content Negotiation
Remove HTML tags from a file to extract only the TEXT
Partition a List in Java
Java – InputStream to Reader
Java Program to Decode a Message Encoded Using Playfair Cipher
Comparing Arrays in Java
Java Program to Implement Circular Singly Linked List
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Mapping Nested Values with Jackson
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Java Program to Create a Random Graph Using Random Edge Generation
HttpClient Basic Authentication
Giới thiệu Json Web Token (JWT)
Converting Java Date to OffsetDateTime
Tránh lỗi NullPointerException trong Java như thế nào?
JPA/Hibernate Persistence Context
Lớp Collectors trong Java 8
The DAO with JPA and Spring
Java Convenience Factory Methods for Collections
Spring Cloud AWS – RDS