Java 将列表元素转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2124324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
converting elements of a list to string
提问by Anto
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?
我想获取存储在列表中的元素的字符串文本,例如List<Car>
. toArray() 和 toString() 方法是最好的选择吗?
采纳答案by Bozho
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)
如果您还没有覆盖该toString()
方法或不想覆盖它,则可以使用car.getName()
代替car.toString()
(或您喜欢的任何属性组合)
回答by Yatendra Goel
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.
注意:只有当你重写了 Car 类的 toString() 方法时,上面才会显示有意义的消息。
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.
toString() 方法应该被覆盖,以字符串形式返回关于对象的有意义的信息。
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.
在你的情况下,我认为有意义的信息是汽车的所有细节。所以覆盖 toString() 方法是最好的方法,而不是使用 getCarName() 或类似的方法。
回答by Dominik
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.
有一种static toString(Object[])
方法 an java.util.Arrays
。用toArray()
List的结果(如您所建议的)调用它应该可以完成这项工作。
回答by Adamski
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.
...您可以简单地调用List
'stoString()
方法来获取您的输出(我不确定为什么人们提倡为此使用循环)。覆盖 Car 的toString()
方法以返回对象的人性化描述也可能是明智的。
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.
但是,如果您希望将每个元素作为单独的元素获取,则String
需要一次遍历 List 一个元素。
回答by Romain Linsolas
Another idea is to use the Apache Commons Langto write the following code:
另一个想法是使用Apache Commons Lang编写以下代码:
StringUtils.join(myList);
The interest is that you also can provide a separator, for example:
有趣的是,您还可以提供分隔符,例如:
StringUtils.join(myList, " ; ");
回答by UnixShadow
First convert your List (Collection) to an array, and create a string of each element.
首先将您的列表(集合)转换为数组,并为每个元素创建一个字符串。
Arrays.toString(myCollection.toArray());
回答by hkbharath
You can use Java 8 Streams
您可以使用 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 JavaDocabout Collectors
for more info.
有关更多信息,请参阅JavaDocCollectors
。
回答by drrob
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.
我不想从 hkbharath 单独给出答案,这是一个很好的答案,但我确实想给出更多使用 Java 8 流的示例。Collectors.joining() 有几个不同的重载变体。
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]
会给你:[福特; 本田;别克]