Collection Utilities in Google Guava

There are many utility classes in Guava which can help achieve collections based operations quicker. Here is a glimpse of the major ones. Note that Convenience Factory Methods for Collections are now a part of Java 9, but these work great if you are still on older versions of Java.

  1. It has utility classes like Lists, Sets, Iterables, Iterators, FluentIterable, Maps, MultiMaps, MultiSets, ObjectArrays, Queues, Streams, Tables etc. which support various utility operations that can be performed on the collections like merging, splitting, converting to different collection type, checking if empty, reversing the sequence etc. as applicable for the type of collection.
  2. Lists
    Provides convenient constructors like newArrayList, newLinkedList etc.
    Provides charactersOf – a shortcut method to get characters of String into List<Character>
    Provides partition and reverse methods to split the List based on size and reverse the List contents. Also has a cartesianProduct implementation to operate between two Lists.
    Provides transform method to convert a List from one Generic Type to other based on a computation performed by a given Function .

    @Test
    public void testLists() {
        List<String> list = Lists.newArrayList("a", "b", "c");
        Assert.assertTrue(list instanceof ArrayList);
    
        System.out.println(Lists.reverse(list));
    
        System.out.println(Lists.transform(list, s -> s.toUpperCase()));
    
        System.out.println(Lists.charactersOf("hello"));
    
        System.out.println(Lists.partition(Lists.charactersOf("elephant"), 3));
    
        System.out.println(Lists.cartesianProduct(Lists.newArrayList("a", "b"), Lists.newArrayList("1", "2")));
    }
  3. Sets
    Like Lists, provides static constructors like newTreeSet, newHashSet etc.
    Provides union, intersection and difference methods to perform between two Set objects.
    Provides implementation for combinations (nCr) and powerSet (all possible subsets for a given set).
    Provides filter method similar to java.util.Stream.

    @Test
    public void testSets() {
        Set<String> set1 = Sets.newHashSet("a", "b", "c");
        Set<String> set2 = Sets.newHashSet("c", "d", "e");
    
        System.out.println(Sets.cartesianProduct(set1, set2));
        Sets.combinations(set1, 2).forEach(s -> System.out.print(s + "; "));
        System.out.println();
        System.out.println(Sets.difference(set1, set2));
        // a, c, e, g etc
        System.out.println(Sets.filter(Sets.union(set1, set2), v -> (v.toCharArray()[0] - 'a') % 2 == 0));
        System.out.println(Sets.union(set1, set2));
        System.out.println(Sets.intersection(set1, set2));
        Sets.powerSet(set1).forEach(s -> System.out.print(s + "; "));
    }
  4. Maps
    Provides static constructors like newHashMap, newTreeMap etc. Even can be constructed using fromProperties method.
    Implements asMap and toMap methods which can be used to generate a Map using a Set / Iterable and a Function to compute a value based on the key provided.
    Implements difference method which gives exhaustive result of difference between two maps with result indicating equality, common entries and different entries represented as com.google.common.collect.MapDifference object.
    Provides exhaustive filtering capabilities like filterKeys, filterEntries and filterValues.
    Provides exhaustive transforming capabilities with transformEntries and transformValues method.

    @Test
    public void testMaps() {
        Set<String> set1 = Sets.newHashSet("a", "b");
        Map<String, String> map1 = Maps.asMap(set1, s -> "A" + s);
        System.out.println(map1);
    
        Map<String, String> map2 = Maps.asMap(set1, s -> "B" + s);
        System.out.println(Maps.difference(map1, map2).entriesInCommon());
        System.out.println(Maps.difference(map1, map2).entriesDiffering());
    
        System.out.println(Maps.filterKeys(map1, s -> s.contains("a")));
    
    }
  5. Iterables
    Provides various utility methods for filtering, finding elements on an Iterable. Also has methods to perform merging, partitioning of different Iterables and many more utility methods.