开发者

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 toAr开发者_JAVA技巧ray() and the toString() methods be the best options?


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜