Converting Elements of a List to String

I would like to obtain the string text of the elements stored in a list, say List<Car>. Would the toArray() and the toString() methods be the best options?

0

8 Answers

Another idea is to use the Apache Commons Lang to write the following code:

StringUtils.join(myList);

The interest is that you also can provide a separator, for example:

StringUtils.join(myList, " ; ");

Providing you don't object to the string output following the convention:

[A, B, C]

... you can simply call the List'stoString() method to obtain your output (I'm not sure why people are advocating using a loop for this). It may also be sensible to override Car's toString() method to return a human-friendly description of the object.

However, if you wish to obtain each element as an individual String you will need to iterate over the List one element at a time.

3

There is a static toString(Object[]) method an java.util.Arrays. Calling it with the toArray() result of the List (as you suggested) should do the job.

3

Yes, but doing it manually gives you more control:

// initialize with the exact length
List<String> stringsList = new ArrayList<String>(listOfCars.size()); 
for (Car car : listOfCars) {
    stringsList.add(car.toString());   
}

If you haven't overridden the toString() method or don't want to override it, you can use car.getName() instead of car.toString() (or any property combination you like)

You can use Java 8 Streams

List<Car> carList = new ArrayList<>();
//Add some elemts to carList
carList.stream()
.map(Car::toString) // maps Car Object to a value returned by toString method
.collect(Collectors.joining(","));

Refer JavaDoc about Collectors for more info.

1

First convert your List (Collection) to an array, and create a string of each element.

Arrays.toString(myCollection.toArray());

I don't want to give a separate answer from hkbharath, which is a good answer, but I do want to give more examples of using Java 8 streams. The Collectors.joining() has a few different overloaded variants.

Let's say that the code in his example:

carList.stream()
.map(Car::toString) // maps Car Object to a value returned by toString method
.collect(Collectors.joining(","));

Gives you this: Ford,Honda,Buick

Then if you don't need a delimiter:

carList.stream()
.map(Car::toString) // maps Car Object to a value returned by toString method
.collect(Collectors.joining());

Would give you instead: FordHondaBuick

And here is an extended example showing how to manipulate each string, change the delimiter, and add a prefix and suffix:

carList.stream()
.map(Car::toString) // maps Car Object to a value returned by toString method
.map(String::toUpperCase) // Transform to upper case
.collect(Collectors.joining("; ", "[", "]"));

Would give you instead: [FORD; HONDA; BUICK]

for (Car car : carsList) {  // carsList is the object of List<Car>
    System.out.println(car);
}

Note: The above will display the meaningful message only when you have overridden the toString() method of Car class.

e.g

public class Car {

    private String carName;

    ....
    ....

    public String toString() {
        return carName;
    }
}

The toString() method should be overridden to return meaningful information about the object in the string form.

In your case, I think the meaningful info would be all the details of the car. So overriding toString() method is best approach instead of using getCarName() or similar methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest