Blog

Top 5 Java libraries for boosting developer productivity in 2024

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 alone. But not all Java libraries are created equal, and with dozens to choose from, finding the right ones to impact your team’s productivity can be challenging.

To make the decision easier, the team at Vaadin has compiled a list of the best Java libraries for productivity. These Java libraries can be dropped into almost any project without introducing a lot of additional 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.

Here are 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?

A Java library is a collection of pre-written classes that provide tools for developers, helping them avoid writing code from scratch. Java libraries enable abstraction and automation of repetitive tasks, allowing developers to focus on solving specific problems.

Java library vs. Java framework

It’s important to differentiate between a Java library and a Java framework. While both speed up development, a Java library allows developers to control when its code is called, whereas a framework dictates the flow and calls the application code.

For example, Vaadin Flow is a Java UI framework, while Jackson is a data-processing Java 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

When paired with Vaadin, these Java libraries become even more powerful. Vaadin Flow, a full-stack framework that enables developers to build web apps 100% in Java, integrates seamlessly with these libraries. This combination ensures long-term scalability, security, and productivity for your Java-based applications.

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