🌐
Baeldung
baeldung.com › home › java › java list › convert an arraylist of object to an arraylist of string elements
Convert an ArrayList of Object to an ArrayList of String Elements | Baeldung
March 7, 2025 - List<String> outputList = new ArrayList<>(inputList.size()); for(Object obj : inputList){ outputList.add(Objects.toString(obj, null)); } Assert.assertEquals(expectedStringList, outputList); 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...
Discussions

Converting a generic list to a string list using Java streams
I’m trying to get the hang of Java’s stream API. I want to make a list of strings from a list of objects. Here’s what I’ve got so far: class Human { String firstName; int yearsOld; } List people = getHumans(); List firstName).collect(Collectors.toList()); But it’s not working right. More on community.latenode.com
🌐 community.latenode.com
0
0
March 20, 2025
Java 8 lambda create list of Strings from list of custom class objects - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
java - Using streams to convert a list of objects into a string obtained from the toString method - Stack Overflow
There are a lot of useful new things in Java 8. E.g., I can iterate with a stream over a list of objects and then sum the values from a specific field of the Object's instances. E.g. public class ... More on stackoverflow.com
🌐 stackoverflow.com
Convert Stream<Object[]> to List<Object> using java stream - Stack Overflow
Really, having a Stream shows a flaw in the design. If you have the chance, you should try to fix the design instead of trying to find a workaround that would make the badly designed solution work anyways. ... List list = ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Attacomsian
attacomsian.com › blog › java-convert-a-list-of-objects-to-list-of-strings
How to convert a list of objects to a list of strings in Java
October 29, 2022 - To convert a list of objects to a list of strings in Java, you can use the Streams API map() method.
🌐
Latenode
community.latenode.com › other questions › api
Converting a generic list to a string list using Java streams - API - Latenode Official Community
March 20, 2025 - Hey everyone! I’m trying to get the hang of Java’s stream API. I want to make a list of strings from a list of objects. Here’s what I’ve got so far: class Human { String firstName; int yearsOld; } List people = getHumans(); List firstName).collect(Collectors.toList()); But it’s not working right.
🌐
javathinking
javathinking.com › blog › java-convert-list-of-object-to-list-of-strings-streams
Java: Convert List of Objects to List of Strings using Streams — javathinking.com
Now, let's see how to convert a list of Person objects to a list of strings using different approaches. import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class ObjectToListOfStrings { public static void main(String[] args) { // Create a list of Person objects List<Person> personList = new ArrayList<>(); personList.add(new Person("Alice", 25)); personList.add(new Person("Bob", 30)); // Convert the list of Person objects to a list of strings using map and collect List<String> stringList = personList.stream() .map(Person::toString) .collect(Collectors.toList()); // Print the list of strings System.out.println(stringList); } }
🌐
Blogger
javarevisited.blogspot.com › 2015 › 03 › 5-ways-to-convert-java-8-stream-to-list.html
5 ways to Convert Java 8 Stream to List - Example, Tutorial
October 6, 2021 - Here are 5 simple ways to convert a Stream in Java 8 to List e.g. converting a Stream of String to a List of String, or converting a Stream of Integer to List of Integer, and so on.
🌐
Mkyong
mkyong.com › home › java › java 8 stream – convert list> to list
Java 8 Stream - Convert List<List<String>> to List<String> - Mkyong.com
July 18, 2019 - package com.mkyong.test; import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; public class Java9Example2 { public static void main(String[] args) { List<String> numbers = Arrays.asList("1", "2", "A", "B", "C1D2E3"); List<String> collect = numbers.stream() .map(x -> new Scanner(x).findAll("\\D+") .map(m -> m.group()) .collect(Collectors.toList()) ) // List<List<String>> .flatMap(List::stream) // List<String> .collect(Collectors.toList()); collect.forEach(x -> System.out.println(x)); } } ... Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities. ... I have a list of object .
Find elsewhere
🌐
Codemia
codemia.io › home › knowledge hub › using streams to convert a list of objects into a string obtained from the tostring method
Using streams to convert a list of objects into a string obtained from the toString method | Codemia
January 27, 2025 - For instance, when you have a list of objects, and you need to transform this list into a string representation of each object, the Stream API can be used to map each object to its toString() representation and then collect these representations into a single string.
🌐
Baeldung
baeldung.com › home › java › java string › converting a list to string in java
Converting a List to String in Java | Baeldung
July 30, 2026 - 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:
🌐
TechWasti
techwasti.com › java-stream-api-convert-list-of-objects-to-another-list-of-objects-using-java-streams
stream API, covert one list of object to another - TechWasti
March 23, 2023 - Interactive simulators, cheat sheets, flashcards, and 2-minute tips for Software Engineers and Architects.
🌐
Makeinjava
makeinjava.com › home › convert list of objects to /from map in java 8 lambda stream (example)
Convert list of objects to /from map in java 8 lambda stream (with example)
January 3, 2024 - ListMapConversion class is responsible for following operations ... Convert Map<String,Person> to List<String> (List of keys).
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java 8 - Converting a List to String with Examples - Java Code Geeks
July 5, 2021 - Next, Collection to String with comma separator or custom delimiter using Java 8 Streams Collectors api and String.join() method. Finally, learn with famous library apache commands StringUtils.join() method. For all the examples, input list must be a type of String as List<String> otherwise we need to convert the non string to String.
Top answer
1 of 5
3

Java 8

You could map them all to Strings first and then group by the tuple (modelId, serviceId) and only then map the results of the grouping to the dtos.

import java.util.Arrays;
import java.util.stream.Collectors;

List<MyDto> myDtos = queryResult.stream()
        // cast all to String and put into tuple
        .map(objects -> Arrays.asList((String) objects[0], (String) objects[1], (String) objects[2]))
         // group by tuple (modelid, serviceId)
        .collect(Collectors.groupingBy(r -> Arrays.asList(r.get(1), r.get(2)),
                Collectors.mapping(r -> r.get(0), Collectors.toList())))
        .entrySet().stream()
        .map(entry -> new MyDto(entry.getKey().get(0), entry.getKey().get(1), entry.getValue()))
        .collect(Collectors.toList());

Bonus points for Java 14+

To increase readability instead of using lists we introduce records

import java.util.List;
import java.util.Map;

record MyDto(String vehicleId, String modelId, List<String> serviceIds) {
    public MyDto(Map.Entry<AggregateKey, List<String>> vehiclesAggregate) {
        this(vehiclesAggregate.getKey().modelId(), vehiclesAggregate.getKey().serviceId(),vehiclesAggregate.getValue());
    }
}

record ResultRow(String vehicleId, String modelId, String serviceId) {
    public ResultRow(Object[] entries) {
        this((String) entries[0], (String) entries[1], (String) entries[2]);
    }

    public AggregateKey getKey() {
        return new AggregateKey(modelId, serviceId);
    }
}

record AggregateKey(String modelId, String serviceId) {}

Resulting in a much shorter and very readable streaming pipeline

import java.util.List;
import java.util.stream.Collectors;

List<MyDto> myDtos = queryResult.stream()
    .map(ResultRow::new)
    .collect(Collectors.groupingBy(ResultRow::getKey,
            Collectors.mapping(ResultRow::vehicleId, Collectors.toList())))
    .entrySet().stream()
    .map(MyDto::new)
    .toList(); // shorthand for collect(Collectors.toList) since Java 16
2 of 5
1

If you don't necessarily need to use stream, you can always use a standard cycle, for example:

Map<String, MyDTO> myDTOs = new HashMap<>();
for(Object[] a : listOfObjectArrays) {
    String key = a[1] + " - " + a[2];
    MyDTO myDTO = myDTOs.get(key);
    if(myDTO == null) {
        List<String> vehicleIds = new ArrayList<>();
        vehicleIds.add((String) a[0]);
        myDTO = new MyDTO((String) a[1], (String) a[2], vehicleIds);
        myDTOs.put(key, myDTO);
    } else {
        myDTO.getVehicleIds().add((String) a[0]);
    }
}

List<MyDTO> myDTOList = new ArrayList<>(myDTOs.values());
🌐
Reddit
reddit.com › r/javahelp › how to convert a list of object to another list with different type of objects?
r/javahelp on Reddit: How to convert a List of object to another list with different type of Objects?
March 22, 2021 -

How can I convert list containing object A to list containing object B. Object A has a method to convert it to object. What is the elegant way in Java 8?

The following code block does not work, but how I make it to work.

protected List<B> getAllB(List<A> alist) { 
   return alist.stream().forEach(s -> s.toADto());
}