eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

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 – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Introduction

In this article, we will look into the different ways we can convert a given ArrayList<Object> to an ArrayList<String>.

2. Problem Statement

Let’s understand the problem statement here. Suppose we have an ArrayList<Object> where the objects can be of any type, ranging from auto-boxed primitive types such as Integer, Float, or Boolean, to non-primitive reference types such as String, ArrayList, HashMap, or even custom-defined classes. We have to write code to convert the said list into an ArrayList<String>. Let’s look at a few examples:

Example 1: [1, 2, 3, 4, 5]
Output 1: ["1", "2", "3", "4", "5"]

Example 2: ["Hello", 4.9837, -19837338737, true]
Output 2: ["Hello", "4.9837", "-19837338737", "true"]

Example 3: [new Node(1,4), new Double(7699.05), new User("John Doe")]
Output 3: ["Node (x=1, y=4)", "7699.05", "User (full name=John Doe)"]

We can provide a host of different objects in the input list, including objects of custom-defined classes like User and Node shown below. It is assumed that these classes have an overridden toString() method. In case the method is not defined, the Object class’s toString() will be invoked, which will generate an output like:

Node@f6d9f0, User@u8g0f9

The above examples contain instances of custom-defined classes such as User and Node:

public class User {
    private final String fullName;

    // getters and setters

   @Override
    public String toString() {
        return "User (" + "full name='" + fullName + ')';
    }
}
public class Node {
    private final int x;
    private final int y;

    // getters and setters

    @Override
    public String toString() {
        return "Node (" + "x=" + x + ", y=" + y + ')';
    }
}

Finally, let’s assume that in the remaining sections, the variables inputList and expectedStringList contain a reference to our desired input and output lists:

List<Object> inputList = List.of(
                        1,
                        true,
                        "hello",
                        Double.valueOf(273773.98),
                        new Node(2, 4),
                        new User("John Doe")
                    );
List<String> expectedStringList = List.of(
                        "1",
                        "true",
                        "hello",
                        Double.toString(273773.98),
                        new Node(2, 4).toString(),
                        new User("John Doe").toString()
                    );

3. Conversion Using Java Collections For-Each Loop

Let’s try to use Java Collections to solve our problem. The idea is to iterate through the elements of the list and convert each element into a String. Once done, we have a list of String objects. In the following code, we iterate through the given list using a for-each loop and explicitly convert every object to a String by calling toString() on it:

List<String> outputList = new ArrayList<>(inputList.size());
for(Object obj : inputList){
    outputList.add(obj.toString());
}
Assert.assertEquals(expectedStringList, outputList);

This solution works for all combinations of objects in the input list and works on all Java versions above Java 5. However, the above solution is not immune to null objects in the input and will throw a NullPointerException whenever it encounters a null. A simple enhancement utilizes the toString() method provided by the Objects utility class introduced in Java 7 and is null-safe:

List<String> outputList = new ArrayList<>(inputList.size());
for(Object obj : inputList){
    outputList.add(Objects.toString(obj, null));
}
Assert.assertEquals(expectedStringList, outputList);

4. Conversion Using Java Streams

We can leverage the Java Streams API to solve our problem as well. We first convert the inputList, our data source, to a stream by applying the stream() method. Once we have a stream of elements, which are of type Object, we need an intermediate operation, which in our case is to do the object-to-string conversion, and finally, a terminal operation, which is to collect the results into another list of type String.

The intermediate operation in our case is a map() operation that takes a lambda expression:

(obj) -> Objects.toString(obj, null)

Finally, our stream needs a terminal operation to compile and return the desired list. In the subsequent subsections, we discuss the different terminal operations available at our disposal.

4.1. Collectors.toList() as a Terminal Operation

In this approach, we use Collectors.toList() to collect the stream generated by the intermediate operation into an output list:

List<String> outputList;
outputList = inputList
    .stream()
    .map((obj) -> Objects.toString(obj, null))
    .collect(Collectors.toList());
Assert.assertEquals(expectedStringList, outputList);

This approach works well for Java 8 and above as the Streams API was introduced in Java 8. The list produced as output here is a mutable list, which means we can add elements to it. The output list can contain null values as well.

4.2. Collectors.toUnmodifableList() as a Terminal Operation – Java 10 Compatible Approach

If we want to produce an output list of String objects that is unmodifiable, we can leverage the Collectors.toUnmodifableList() implementation introduced in Java 10:

List<String> outputList;
outputList = inputList
    .stream()
    .filter(Objects::nonNull)
    .map((obj) -> Objects.toString(obj, null))
    .collect(Collectors.toUnmodifiableList());
Assert.assertEquals(expectedStringListWithoutNull, outputList);

An important caveat here is that the list cannot contain null values and, hence, if the inputList contains a null, this code produces a NullPointerException. This is why we add a filter to select only nonNull elements from the stream before applying our operation. The outputList is immutable and will produce an UnsupportedOperationException if there is an attempt to modify it later.

4.3. toList() as a Terminal Operation – Java 16 Compatible Approach

In case we want to directly create an unmodifiable list from the input Stream, but we want to allow null values in the resulting list, we can use the toList() method that was introduced in the Stream interface in Java 16:

List<String> outputList;
outputList = inputList
    .stream()
    .map((obj) -> Objects.toString(obj, null))
    .toList();
Assert.assertEquals(expectedStringList, outputList);

5. Conversion Using Guava

We can use the Google Guava Library to transform the input list of objects into a new list of String.

5.1. Maven Configuration

To use the Google Guava library, we need to include the corresponding Maven dependency in pom.xml:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>33.0.0-jre</version>
    <scope>test</scope>
</dependency>

The latest version of the dependency can be obtained from Maven Central.

5.2. Using Lists.transform()

We can use the transform() method in the Google Guava Lists class. It takes in the inputList and the aforementioned lambda expression to generate the outputList of String objects:

List<String> outputList;
outputList = Lists.transform(inputList, obj -> Objects.toString(obj, null));
Assert.assertEquals(expectedStringList, outputList);

With this method, the output list can contain null values.

6. Conclusion

In this article, we looked into a few different ways an ArrayList of Object elements can be converted into an ArrayList of String. We explored different implementations from a for-each loop-based approach to a Java Streams-based approach. We also looked at different implementations specific to different Java versions, as well as one from Guava.

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.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

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.

eBook Jackson – NPI EA – 3 (cat = Jackson)