Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

Thymeleaf is a Java template engine for processing and creating HTML.

In this quick tutorial, we’ll look into Thymeleaf’s lists utility object to perform common list-based operations.

2. Computing Size

First, the size method returns the length of a list. We can include it, say, via the th:text attribute:

size: <span th:text="${#lists.size(myList)}"/>

myList is our own object. We’d have passed it via the controller:

@GetMapping("/size")
public String usingSize(Model model) {
    model.addAttribute("myList", getColors());
    return "lists/size";
}

3. Checking If the List Is Empty

The isEmpty method returns true if the given list has no elements:

<span th:text="${#lists.isEmpty(myList)}"/>

Generally, this utility method is used with conditionals – th:if and th:unless:

<span th:unless="${#lists.isEmpty(myList)}">List is not empty</span>

4. Checking Membership

The contains method checks whether an element is a member of the given list:

myList contains red: <span th:text="${#lists.contains(myList, 'red')}"/>

Similarly, we can check the membership of multiple elements using the containsAll method:

myList contains red and green: <span th:text='${#lists.containsAll(myList, {"red", "green"})}'/>

5. Sorting

The sort method enables us to sort a list:

sort: <span th:text="${#lists.sort(myList)}"/>

sort with Comparator: <span th:text="${#lists.sort(myList, reverse)}"/>

Here we have two overloaded sort methods. Firstly, we’re sorting our list in the natural order – ${#lists.sort(myList)}. Secondly, we’re passing an additional parameter of type Comparator. In our example, we’re getting this comparator from the model.

6. Converting to List

Lastly, we can convert Iterables and arrays to Lists using the toList method.

<span th:with="convertedList=${#lists.toList(myArray)}">
    converted list size: <span th:text="${#lists.size(convertedList)}"/>
</span>

Here we’re creating a new List, convertedList, and then printing its size with #lists.size.

7. Summary

In this tutorial, we’ve investigated the Thymeleaf built-in lists utility object and how to use it effectively.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW