将 List 转换为 String

将 List 转换为 String的几种方式
1、使用toString()方法将 List 转换为 String
2、使用Java 8 Streams Collectors api和String.join()方法将带有逗号分隔符或自定义分隔符的集合转换为字符串。
3、 apache 库命令StringUtils.join()方法。
对于所有示例,输入列表必须是 String 作为List类型,否则我们需要将非字符串转换为 String。例如,List 是 Double 类型,然后需要先将 double 转换为字符串。

使用标准 toString() 方法
List.toString()是最简单的,但它在开头和结尾添加方括号,每个字符串用逗号分隔符分隔。
缺点是我们不能用另一个分隔符替换逗号,也不能去掉方括号。

/**

  • Example to convert List to string using toString() method.
  • @author javaprogramto.com

*/
public class ListToStringUsingToStringExample {

public static void main(String args) {
    
// creating a list with strings.
List<String> list = Arrays.asList("One",
                  "Two",
                  "Three",
                  "Four",
                  "Five");

// converting List<String> to String using toString() method
String stringFromList = list.toString();

// priting the string
System.out.println("String : "+stringFromList);        
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
输出:String : [One, Two, Three, Four, Five]
1
Java 8 String.join()
java 8 String添加了一个特殊的方法String.join()以将集合转换为具有给定分隔符的字符串。

public class ListToStringUsingString_JoinExample {

public static void main(String args) {
    
// creating a list with strings.
List<String> list = Arrays.asList("One",
                  "Two",
                  "Three",
                  "Four",
                  "Five");

// converting List<String> to String using toString() method
String stringFromList = String.join("~", list);

// priting the string
System.out.println("String with tilde delimiter: "+stringFromList);

// delimiting with pipe | symbol.
String stringPipe = String.join("|", list);

// printing
System.out.println("String with pipe delimiter : "+stringPipe);

}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
输出:
String with tilde delimiter: OneTwoThreeFourFive
String with pipe delimiter : One|Two|Three|Four|Five
1
2
3
Collectors.joining()
Collectors.join()方法来自 java 8 stream api。Collctors.joining()方法将分隔符、前缀和后缀作为参数。此方法将列表转换为具有给定分隔符、前缀和后缀的字符串。
查看以下有关使用不同分隔符的 join() 方法的示例。但是,String.join() 方法不提供前缀和后缀选项。
如果您需要自定义分隔符、前缀和后缀,请使用这些。如果您不想要前缀和后缀,则提供空字符串以不在结果字符串前后添加任何内容。

public class ListToStringUsingString_JoinExample {

public static void main(String args) {
    
// creating a list with strings.
List<String> list = Arrays.asList("One",
                  "Two",
                  "Three",
                  "Four",
                  "Five");

// using java 8 Collectors.joining with delimiter, prefix and suffix
String joiningString = list.stream().collect(Collectors.joining("-", "{", "}"));

// printing
System.out.println("Collectors.joining string : "+joiningString);

String joiningString3 = list.stream().collect(Collectors.joining("@", "", ""));

// printing
System.out.println("Collectors.joining string with @ separator : "+joiningString3);


}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
输出:
Collectors.joining string : {One-Two-Three-Four-Five}
Collectors.joining string with @ separator : One@Two@Three@Four@Five
1
2
3
Apache Commons StringUtils.join()
最后的方法是使用来自 apache commons 包的外部库。该库有一个方法StringUtils.join() ,它采用类似于 String.join() 方法的列表和分隔符。

public class ListToStringUsingStringUtils_JoinExample {

public static void main(String args) {
    
// creating a list with strings.
List<String> list = Arrays.asList("One",
                  "Two",
                  "Three",
                  "Four",
                  "Five");

// using java 8 Collectors.joining with delimiter, prefix and suffix
String joiningString = StringUtils.join(list, "^");

// printing
System.out.println("StringUtils.join string with ^ delimiter : "+joiningString);

String joiningString3 = StringUtils.join(list, "$");

// printing
System.out.println("StringUtils.join string with @ separator : "+joiningString3);


}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
输出:
StringUtils.join string with ^ delimiter : OneTwoThreeFourFive
StringUtils.join string with @ separator : OneTwoThreeFourFive

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容