Blog

The 5 best Java libraries for developer productivity

By  
Matti Tahvonen
Matti Tahvonen
·
On May 7, 2024 6:19:00 PM
·

Java productivity libraries

Java libraries enable developers to be more productive than with pure JDK. But not all Java libraries are created equal, and with dozens to choose from, finding the one(s) that will make the biggest impact on your team’s productivity can be a tough task.

To help make the decision easier, the team here at Vaadin put our heads together to compile a list of what we consider the best Java libraries for productivity. (After all, productivity is at the heart and soul of everything we do.) You can drop these libraries into almost any project without a gazillion other transitive dependencies. 

We have left out libraries that we consider to be more like framework features, such as dependency injection frameworks or, for example, Spring Data.


Our top 5 favorite Java libraries for productivity: 

  1. Project Lombok
  2. Apache Commons libraries
  3. Jsoup
  4. Mockito
  5. Ehcache

What is a Java library?

As a broad definition, a Java library is a collection of classes that provide tools so developers don't have to write these tools from scratch. It facilitates the abstraction and automation of application functionalities. Developers can use the library for everyday tasks and focus their time and energy on solving specific problems related to their use case instead. 

Java library vs. Java framework

You may have noticed that both terms library and framework are used frequently in Java forums. Although both are often used interchangeably, they are technically different. Both libraries and frameworks are a collection of Java code to speed up the development process. The critical difference is that the framework controls the order of code events when used.

If you use a library, the application code determines when to call the library code for a specific task. When you use a framework, you write application code according to the framework's requirements. The framework calls the application code and any libraries as and when required.

For example, Vaadin Flow is a Java UI framework, and Jackson is a data processing library. You can learn more about the top Java UI frameworks here.

Top 5 Java Libraries for developer productivity

Ready to improve your productivity with a Java library? Here are our top 5 choices.

1. Project Lombok

Project Lombok is a Java library that plugs into your editor and build tools, making your code less verbose and easier to understand. Your class has a fully featured builder with one annotation. You can automate your logging variables, use APIs for object creation, throw checked exceptions, and much more!

@Entity
@Getter @Setter @NoArgsConstructor
public class Competition extends AbstractEntity {
   private String name;
   private LocalDateTime start;
   private LocalDateTime end;
   private boolean open;
   private Integer startIntervalSeconds = 60;
   @OneToMany(mappedBy="competition", cascade = CascadeType.ALL)
   private List<SeriesGroup> seriesGroups;
}

The code block shows a JPA entity whose required getters, setters, and no-argument constructors are implemented automatically using Lombok. The @Getter, @Setter, and @NoArgsConstructor are from Lombok.

Productivity benefits

Due to Java's rather verbose conventions, we have all become masters at writing getters, setters, constructors, etc. IDE features that generate these methods are in the toolbox of most Java developers, but maintaining those methods, especially in the early phases of development, can be a burden.

Project Lombok can be your secretary for many tasks that need mechanical coding. For example, based on annotation processing, it can maintain your getters, setters, constructors, toString methods, and so on. Your data classes will look much simpler in the editor, and refactoring will look very clear in the GIT history.

Watch out for

Lombok can cause problems if you don’t understand what it is doing. For example, the equals and hashCode implementations it can write for you make your data objects incompatible with standard JDK collection classes such as lists, sets, and maps. It is also impossible to add JavaDocs to Lombok's methods.

When to choose

Lombok can significantly boost productivity, especially at the beginning of the project. We also recommend Lombok for IntelliJ users because the tooling is built-in with the latest versions.

Lombok has a Delombok feature that removes all Lombok annotations from your code and replaces them with the generated code. We recommend starting with Lombok, but when your project becomes more stable, use Delombok to make debugging easier.

Getting started

Lombok can be added to your project with the following Maven dependency

<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <version>1.18.24</version>
   <scope>provided</scope>
</dependency>

2. Apache Commons libraries

Apache Commons is a project focused on all aspects of reusable Java components. Developers from the Apache community collaborate to build the library for everyone’s benefit. It is composed of three parts:

  1. The Commons Proper - The main repository of Java components.
  2. The Commons Sandbox - A workspace for new Java component development.
  3. The Commons Dormant - A repository of inactive components.

The most notable libraries a productive Java developer should know about are commons-lang3 and commons-collections.

Guava is another library similar to the commons. Instead of being split into multiple libraries, it has a larger productivity library with a single dependency. Google’s Java developers largely maintain Guava, and a version of the library works in Android development.

Productivity benefits

The Apache Foundation has a long history of complementing JDK’s built-in general-purpose libraries with various common libraries. Many handy shorthands and helper methods, such as string and input and output stream handling, are available. Some of these have become less relevant recently as JDK has developed, but they still greatly enhance Java’s core libraries.

When to choose

We recommend it for:

  • Input/output tasks, specifically FileUtils and IOUtils—Commons handles the repetitive work, making the code quicker to write and more precise to read.
  • Date and time handling—DateUtils is simpler than Java's built-in Date and Calendar classes.
  • String processing—StringUtils is null-safe and makes searching for specific patterns within your strings easy.

Getting started

Maven snippet for Commons Lang:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

Maven snippet for Commons Collections:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.4</version>
</dependency>

3. Jsoup

Jsoup is a Java library developers use to parse, extract, and manipulate HTML data. It offers APIs for fetching HTML documents from the web, manipulating them, and extracting data using DOM traversal or CSS selectors. Jsoup code for extracting information from HTML DOM is compact, efficient, and easy to read.

Productivity benefits

HTML has become the de facto standard as a rich text format. Even if your application isn’t actually a web app or your web framework is Vaadin, which allows you to write web apps without a single line of HTML, there is a high chance that, at some point, you will need to get involved in HTML markup. Jsoup parses the HTML and allows programmatic manipulation.

 It is an essential tool for web scraping and data extraction. It provides a practical interface to work with complex HTML structures without the overhead of manual parsing.

When to choose

A good reason to master Jsoup is security. Its parsing of the document object model makes it a very safe way to sanitize HTML input from untrusted sources.

We use Jsoup at Vaadin in our Flow framework, so Vaadin developers get it as a transitive dependency in their project. 

HTML has become the de facto standard as a rich text format. Even if your application isn’t actually a web app or your web framework is Vaadin, which allows you to write web apps without a single line of HTML, there is a high chance that at some point you will need to get involved in HTML markup. Jsoup parses the HTML and allows programmatic manipulation.

A good reason for mastering Jsoup is security. The fact that it actually parses the document object model makes it a very safe way to sanitize HTML input coming from untrusted sources.

getElement().setProperty("innerHTML",
  Jsoup.clean(
    "<b onclick=\"alert('see no evil')\">All good!</b>",
    Safelist.basic())
);

The above code block shows how to use Jsoup to clean “evil Javascript” from HTML to set up a Vaadin component.

Getting started

Maven snippet for Jsoup:

<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.15.1</version>
</dependency>

4. Mockito

Mocking is a unit testing technique that allows you to verify the behavior of smaller units of your application. Mocks replace other components during the test and still allow you to verify the correct behavior. 

Mockito offers a clean, intuitive API for stubbing, verifying interactions, and ensuring that components interact as expected.

Productivity benefits

The Mockito library helps you write mocks much faster than you can by doing it manually. It enhances test readability and maintenance by providing a clear and declarative way to define mock behavior and expectations. You can effectively isolate and test individual application components—avoid writing unnecessary integration tests that slow down your testing process.

When to choose

Mockito has become almost a standard library for many professional Java developers. It is particularly useful when testing components interacting with databases, network services, or third-party libraries. You can simulate these system components cleanly and efficiently.  Choose Mockito when you want to:

  • Verify interactions between components without involving actual dependencies for faster and more focused tests.
  • Automate and simplify testing of complex interactions within the code.
  • Try test-driven development—write tests first and code afterward.

You can also try behavior-driven development syntax with BDDMockito.

Getting started

Maven snippet for Mockito.

<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-core</artifactId>
  <version>4.5.1</version>
</dependency>

5. Ehcache

Ehcache is an open-source Java-based cache that effectively improves application performance by reducing database load and increasing speed. It stores frequently accessed data in memory for quicker retrieval without constantly querying a database. 

Ehcache supports various caching strategies, such as in-memory, on-disk, and clustered caching. It is handy for quick in-memory caching, but you can also configure it to push rarely accessed items to the disk if your cache needs to be large.

Productivity benefits

“There are only two hard things in Computer Science: cache invalidation and naming things.”—Phil Karlton. 

We probably won’t ever be able to solve the naming problem completely, but with cache invalidation, an advanced library might help. Ehcache is one of the most popular ones in the JVM ecosystem. It transparently handles the complexities of data caching so developers can implement caching strategies without deep expertise in caching mechanics.  It also reduces the need for complex database optimizations so you can focus more on core application logic. As applications grow, Ehcache provides scalable caching solutions to manage increasing loads without substantial architectural changes.

When to choose

Ehcache's capabilities make it ideal for applications requiring high performance, scalability, and data availability. Use it if:

  • Your application frequently accesses slow or costly resources like databases.
  • Your application makes repetitive database queries for the same data.
  • You must store temporary data, like session information, for a short period.

Getting started

In simple cases, you can probably use Ehcache as an implementation of the standard JCache API to keep a direct dependency on Ehcache away from your application. Ehcache can also be easily distributed to multiple JVMs in your cluster using the Terracotta server for more complex use cases.

Maven dependency for Ehcache:

<dependency>
      <groupId>org.ehcache</groupId>
      <artifactId>ehcache</artifactId>
      <version>3.10.0</version> 
    </dependency>

Pair Java libraries with Vaadin for even more productivity

All Java libraries introduced in this article are stable software proven in production. But remember that every dependency you add to your application makes it a bit more complex. Discuss with your team which libraries you want to use and the policies regarding adding new ones!

When developing a new app, choose technologies that ensure long-term effectiveness and scalability. Vaadin Flow is a unique full-stack framework that lets you build web apps  100% in Java without writing HTML or JavaScript. Java libraries integrated with Vaadin offer a mature technology stack spanning the data layer to the user interface. Your team can boost productivity without compromising on data security. You can build applications faster and ensure they last for decades without maximizing maintenance expenses.

New to Vaadin? Take it for a spin and get started with a new project in minutes at start.vaadin.com.

Matti Tahvonen
Matti Tahvonen
Matti Tahvonen has a long history in Vaadin R&D: developing the core framework from the dark ages of pure JS client side to the GWT era and creating number of official and unofficial Vaadin add-ons. His current responsibility is to keep you up to date with latest and greatest Vaadin related technologies. You can follow him on Twitter – @MattiTahvonen
Other posts by Matti Tahvonen