Java列表串
在本教程中,我们将看到如何将列表转换为Java中的字符串。
以下是将列表转换为字符串的方法:
使用StringBuilder.
我们可以用 StringBuilder类要将列表转换为字符串。
StringBuilder是最好的方法,如果我们有字符串数组,列表。
我们可以使用stringBuilder对象中的元素使用 append()方法时循环,然后将其转换为字符串使用 toString()终端字符串类的方法。
例子:
package org.igi.theitroad;
import java.util.List;
import java.util.ArrayList;
import java.lang.String;
public class ConvertListToStringMain
{
public static void main(String arg[])
{
//creation and initialization of list
List<String> list=new ArrayList<>();
list.add("Mary");
list.add("Martin");
list.add("John");
list.add("Newton");
//print content of list
System.out.print("Elements of list are:");
System.out.println(list);
//conversion of ArrayList to String
StringBuilder strbul=new StringBuilder();
for(String str : list)
{
strbul.append(str);
//for adding comma between elements
strbul.append(",");
}
//just for removing last comma
//strbul.setLength(strbul.length()-1);
String str=strbul.toString();
System.out.println("Converted String is " + str);
}
}
输出:
Elements of list are:[Mary, Martin, John, Newton] Converted String is Mary,Martin,John,Newton,
使用+运算符
这不是一种有效的方式,因此,通常避免它。
我们使用concate( +)运算符从阵列循环中逐个元素添加一个元素,并将其分配到字符串中。
例子:
package org.igi.theitroad;
//import all these packages
import java.util.List;
import java.util.ArrayList;
import java.lang.String;
import java.util.Arrays;
public class ListToStringPlusMain
{
public static void main(String arg[])
{
//creation and initialization of list
List list=new ArrayList<>();
list.add("Mary");
list.add("Martin");
list.add("John");
list.add("Newton");
//print content of list
System.out.print("Elements of list are:");
System.out.println(list);
//conversion of ArrayList into String using +
String str="";
for(String s : list)
{
str=str + s + ",";
}
System.out.println("Converted String is " + str);
}
}
在Java 8中使用String的加入方法
在Java 8中引入了String的连接方法。
此方法非常方便地从列表中创建分隔的分隔字符串。
package org.igi.theitroad;
//import all these packages
import java.util.List;
import java.util.ArrayList;
import java.lang.String;
public class ListToStringJoinMain
{
public static void main(String arg[])
{
//creation and initialization of list
List list=new ArrayList<>();
list.add("Mary");
list.add("Martin");
list.add("John");
list.add("Newton");
//print content of list
System.out.print("Elements of list are:");
System.out.println(list);
//conversion of ArrayList into String using +
String str=String.join(",", list);
System.out.println("Converted String is " + str);
}
}
此方法仅适用于字符串列表。
如果我们有double列表,那么我们可以使用Java 8的收集器加入字符串。
这是一个例子:
package org.igi.theitroad;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ConvertListToStringMain
{
public static void main(String arg[])
{
//creation and initialization of list
List<Double> list=new ArrayList<>();
list.add(34.2);
list.add(29.7);
list.add(40.2);
list.add(50.4);
//print content of list
System.out.print("Elements of list are:");
System.out.println(list);
//conversion of ArrayList into String using Java 8's Collector
String str = list.stream().map(Object::toString)
.collect(Collectors.joining(","));
System.out.println("Converted String is: " + str);
}
}
输出:
Elements of list are:[34.2, 29.7, 40.2, 50.4] Converted String is: 34.2,29.7,40.2,50.4
正如我们所看到的,我们使用过 Collectors.joining(",")加入字符串的元素
使用apache common lang3
我们也可以使用apache common的 StringUtils将列表转换为字符串。
以下是我们需要为Apache Common Lang3添加的依赖项 pom.xml。
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency>
这是一个例子:
package org.igi.theitroad;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
public class ConvertStringToListApache
{
public static void main(String arg[])
{
List<Integer> intList= Arrays.asList(new Integer[] {1,2,3,4});
//print content of list
System.out.print("Elements of list are:");
System.out.println(intList);
String join = StringUtils.join(intList,"*");
System.out.println("Converted String is: " + join);
}
}
使用番石榴库
你也可以使用番石榴 Joiner类要将列表转换为字符串。
以下是我们需要为Apache Common Lang3添加的依赖项 pom.xml。
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>29.0-jre</version> </dependency>
这是一个例子:
package org.igi.theitroad;
import java.util.Arrays;
import java.util.List;
import com.google.common.base.Joiner;
public class ConvertListToStringGuava
{
public static void main(String arg[])
{
List<Integer> intList= Arrays.asList(new Integer[] {1,2,3,4});
//print content of list
System.out.print("Elements of list are:");
System.out.println(intList);
String join = Joiner.on("*").join(intList);
System.out.println("Converted String is: " + join);
}
}
正如我们所看到的,我们使用过 com.google.common.base.Joiner类加入字符串的元素 *
打印列表的元素作为字符串
有时,我们只需要打印列表的元素。
我们可以简单地使用 toString()方法让我们在举例的帮助下了解。
package org.igi.theitroad;
import java.util.Arrays;
import java.util.List;
public class PrintListOfElementsMain
{
public static void main(String arg[])
{
List<Integer> intList= Arrays.asList(new Integer[] {1,2,3,4});
//print content of list
System.out.print("Elements of list are:");
System.out.println(intList);
}
}
你注意到我们甚至没有调用 toString()Intllist对象的方法?
JVM内部调用此列表中元素类型的ToString()方法。
在这种情况下,我们有整数列表,因此它会调用整数 toString()方法。
如果,我们有自定义对象,那么我们应该覆盖ToString()方法,否则它将为我们提供意外结果。
让我们在exclue创建一个名为的简单类的帮助下看 Color.java
package org.igi.theitroad;
public class Color {
String name;
String htmlCode;
public Color(String name, String htmlCode) {
super();
this.name = name;
this.htmlCode = htmlCode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHtmlCode() {
return htmlCode;
}
public void setHtmlCode(String htmlCode) {
this.htmlCode = htmlCode;
}
}
创建名为的主类 PrintListOfColorsMain.java
package org.igi.theitroad;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class PrintListOfColorsMain {
public static void main(String[] args) {
//Sort list of colors by name
List<Color> listOfColors = getListOfColors();
System.out.println(listOfColors);
}
public static List<Color> getListOfColors() {
List<Color> listOfColors = new ArrayList<>();
Color red = new Color("Red", "#FF0000");
Color blue = new Color("Blue", "0000FF");
Color white = new Color("White", "#FFFFFF");
Color green = new Color("Green", "#008000");
listOfColors.add(red);
listOfColors.add(blue);
listOfColors.add(white);
listOfColors.add(green);
return listOfColors;
}
}
如果我们注意到,我们没有 toString()方法 Color类,这就是为什么它正在调用对象的ToString()方法。
我们需要覆盖 toString()方法 Color类,我们将获得信息性的输出。
package org.igi.theitroad;
public class Color {
String name;
String htmlCode;
public Color(String name, String htmlCode) {
super();
this.name = name;
this.htmlCode = htmlCode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHtmlCode() {
return htmlCode;
}
public void setHtmlCode(String htmlCode) {
this.htmlCode = htmlCode;
}
@Override
public String toString() {
return "Color [name:"+name+" HtmlCode:"+htmlCode+"]";
}
}

