文章目录
1.`joining()`方法说明2.使用`Collectors.joining()`方法拼接包含`String`类型的`List`2.使用`Collectors.joining()`方法拼接包含`Object`类型的`List`参考文献在本页中,我们将提供java 8 Collectors joining()
示例。方法返回收集器实例。它按顺序连接输入元素。我们也可以在元素连接中传递分隔符、前缀和后缀。
1.joining()
方法说明
假设我们用以下方式收集元素A、B、C、D。
①
joining()
它返回用于连接不带分隔符的元素的收集器。对于给定的输入元素,输出字符串将是ABCD。
②
joining(CharSequence delimiter)
它返回用于将元素与给定分隔符连接起来的收集器。对于给定的输入元素和分隔符(-),输出字符串将是A-B-C-D。
③
joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
它返回收集器,该收集器用于连接具有给定分隔符、前缀和后缀的元素。对于给定的输入元素,分隔符( - )、前缀( [ )和后缀( ] ),输出字符串将是[A-B-C-D]。
2.使用Collectors.joining()
方法拼接包含String
类型的List
JoiningExampleWithListOfString.java
package com.concretepage;import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;public class JoiningExampleWithListOfString {public static void main(String[] args) {List<String> list = Arrays.asList("Ram","Shyam","Shiv","Mahesh");String result= list.stream().collect(Collectors.joining());System.out.println(result);result= list.stream().collect(Collectors.joining(","));System.out.println(result);result= list.stream().collect(Collectors.joining("-","[","]"));System.out.println(result); }}
输出
RamShyamShivMaheshRam,Shyam,Shiv,Mahesh[Ram-Shyam-Shiv-Mahesh]
2.使用Collectors.joining()
方法拼接包含Object
类型的List
JoiningExampleWithListOfObject.java
package com.concretepage;import java.util.List;import java.util.stream.Collectors;public class JoiningExampleWithListOfObject {public static void main(String[] args) {List<Person> list = Person.getList();System.out.println("--Join person name--");String result= list.stream().map(p -> p.getName()).collect(Collectors.joining());System.out.println(result);result= list.stream().map(p -> p.getName()).collect(Collectors.joining("|"));System.out.println(result);result= list.stream().map(p -> p.getName()).collect(Collectors.joining("-","[","]"));System.out.println(result);System.out.println("--Join person age--");result= list.stream().map(p -> String.valueOf(p.getAge())).collect(Collectors.joining());System.out.println(result);result= list.stream().map(p -> String.valueOf(p.getAge())).collect(Collectors.joining("|"));System.out.println(result);result= list.stream().map(p -> String.valueOf(p.getAge())).collect(Collectors.joining("-","[","]"));System.out.println(result); System.out.println("--Join person name-age--");result= list.stream().map(p -> p.getName()+"-" + p.getAge()).collect(Collectors.joining("|"));System.out.println(result);result= list.stream().map(p -> p.getName()+"-" + p.getAge()).collect(Collectors.joining("|","[","]"));System.out.println(result); }}
Person.java
package com.concretepage;import java.util.ArrayList;import java.util.List;public class Person {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public int getAge() {return age;}public static List<Person> getList() {List<Person> list = new ArrayList<>();list.add(new Person("Ram", 23));list.add(new Person("Shyam", 20));list.add(new Person("Shiv", 25));list.add(new Person("Mahesh", 30));return list;}}
输出
--Join person name--RamShyamShivMaheshRam|Shyam|Shiv|Mahesh[Ram-Shyam-Shiv-Mahesh]--Join person age--2320253023|20|25|30[23-20-25-30]--Join person name-age--Ram-23|Shyam-20|Shiv-25|Mahesh-30[Ram-23|Shyam-20|Shiv-25|Mahesh-30]
参考文献
【1】Java 8 Doc: Collectors
【2】Java 8 Collectors: joining() Example
如果觉得《【Java 8 新特性】Java 8 Collectors:joining()使用示例》对你有帮助,请点赞、收藏,并留下你的观点哦!