Should contain the Java collection framework Multimap

Apache Commons Collections versus Google Guava

1. Overview

This tutorial will be two Java-based open source libraries compared : Apache Commons and Google Guava . Both libraries are rich in functionality with many utility APIs, mostly in the collections and I / O area.

For the sake of brevity, we're only going to describe a handful of the most commonly used ones from the collection framework, along with code examples. We will also see a summary of their differences.

Additionally have we have a collection of articles for an in-depth look at various commons and guava utilities .

2. A brief history of the two libraries

Google Guava is a Google project that was mainly developed by the company's engineers, although it is now open source. The The main motivation for the start was to bring generics introduced in JDK 1.5 into the Java Collections Framework (JCF) and improve its functions.

Since its inception, the library has expanded its capabilities to include diagrams, functional programming, area objects, caching, and String Manipulation.

Apache Commons started as a Jakarta project to complement the Java Core Collection API and eventually became a project of the Apache Software Foundation. Over the years it has expanded into a vast repertoire of reusable Java components in various other areas including, but not limited to, imaging, I / O, cryptography, caching, networking, validation, and object pooling.

Since this is an open source project, developers from the Apache community are constantly adding to this library to add functionality. you however, take great care to maintain backward compatibility .

3. Maven dependency

To include guava, we must add its dependence to ours pom.xml Add :

Please visit Maven for the latest version information.

It's a little different for Apache Commons. Depending on the utility we want to use, we need to add this particular one. For example, for collections we need to add:

In our code examples we use commons-collection4 .

Now let's jump into the fun part!

4. Bidirectional cards

Cards that can be accessed by their keys, as well as values, are called bidirectional cards. JCF does not have this capability.

Let's see how our two technologies deliver. In both cases we take an example of days of the week to get the name of the day based on its number and vice versa.

4.1. Guava is bimap

Guava provides an interface - BiMap - as a bidirectional card. It can be with any of its implementations EnumBiMap , EnumHashBiMap , HashBiMap or ImmutableBiMap instantiated become .

Here we use HashBiMap :

Padding is similar to any card in Java:

And here are some JUnit tests to prove the concept:

4.2. Apaches BidiMap

Apache also provides us with his BidiMap- Surface available:

Here we use TreeBidiMap . However, there are other implementations, like DualHashBidiMap and DualTreeBidiMap also .

To it too to fill , we can use the values ​​as above for Set BiMap .

The usage is pretty similar too:

In some simple performance tests, this bidirectional card stayed back behind their guava counterpart only when inserting. Retrieving keys and values ​​was much faster .

5. Assign keys to multiple values

For a use case in which we want to assign several keys to different values, e.g. For example, a collection of shopping trolleys for fruit and vegetables, the two libraries offer us unique solutions.

5.1. Guava des MultiMap

First, let's see how you do MultiMap instantiate and initialize:

Then we're going to use some JUnit tests to see it in action:

Furthermore can we with MultiMap remove a specific entry or a whole set of values ​​from the map :

As we can see, here we go first Apple from the Fruit set and then the whole Fruit set removed .

5.2. Apaches MultiValuedMap

Let's start again with instantiating a MultiValuedMap :

Since padding is the same as in the previous section, let's take a quick look at how it is used:

As we can see, its usage is the same too!

In this case, however, we don't have the flexibility to like a single entry Apple out Fruits to remove . We can only do the whole sentence fruit remove :

6. Assign several keys to a value

Here we take an example of latitude and longitude that should be assigned to the respective cities:

Now we shall see how this can be achieved.

6.1. Guava the table

Guava offers one Table that fulfills the above application:

And here are some uses we can infer from it:

As we can see, we can Set- Get view of rows, columns and values.

The table also gives us the option to query your rows or columns .

Let's look at a movie table to demonstrate this:

And here are some self-explanatory examples that we can do on ours Movies table :

However, Table limits us to map only two keys to a value. We don't have an alternative as yet in Guava to map more than two keys to a single value.

6.2. Apache's MultiKeyMap

Coming back to our cityCoordinates example, here's how we can manipulate it using MultiKeyMap:

As we can see from the above code snippet, to arrive at the same assertions as for Guava's Table, we had to iterate over the MultiKeyMap.

However, MultiKeyMap Also offers the possibility to map more than two keys to a value. For example, it gives us the ability to map days of the week as weekdays or weekends:

7. Apache Commons Collections vs. Google Guava

As per its engineers, Google Guava was born out of the need to use generics in the library, which Apache Commons didn't offer. It also follows the collections API requirements to the tee. Another major advantage is that it's in active development with new releases coming out frequently.

However, Apache offers an edge when it comes to performance while fetching a value from a collection. Guava still takes the cake though, in terms of insertion times.

Although we compared only the collections APIs in our code samples, Apache Commons as a whole offers a much bigger gamut of features as compared to Guava.

8. Conclusion

In this tutorial, we compared some of the functionality offered by Apache Commons and Google Guava, specifically in the area of ​​the collections framework.

Here we have only scratched the surface of what the two libraries have to offer.

Furthermore, it is not an either / or comparison. As our code examples have shown there are functions that are unique to each of the two functions, and there may be situations in which the two can coexist .

As always, the source code is available on GitHub.