方法定义
Java 8 流 ( stream ) 收集器 ( Collectors ) 中的joining()
方法会返回一个Collectors
实例,方便在流收集器上的链式操作。
Collectors.joining()
方法以遭遇元素的顺序拼接元素。我们可以传递可选的拼接字符串、前缀和后缀。
假设我们的流中有四个元素["A","B","C","D"]
,那么我们就可以按照以下方式来收集它们。
无参方法
joinning()
无参数方法会返回一个Collectors
实例,并且以空字符串 (""
) 来拼接收集到的所有元素
package com.sjh.test.java8;importjava.util.Arrays;importjava.util.List;importjava.util.stream.Collectors;publicclassJoiningTest1{publicstaticvoidmain(String[] args){List<String> list= Arrays.asList("1","2","3","4");String result= list.stream().collect(Collectors.joining());System.out.println(result);}}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.JoiningTest11234Process finished with exitcode 0
单个参数
joining(CharSequence delimiter)
接受一个参数字符串序列作为拼接符,并返回一个Collectors
实例。假如我们传递的拼接符为"-"
。那么输出结果为1-2-3-4
package com.sjh.test.java8;importjava.util.Arrays;importjava.util.List;importjava.util.stream.Collectors;publicclassJoiningTest2{publicstaticvoidmain(String[] args){List<String> list= Arrays.asList("1","2","3","4");String result= list.stream().collect(Collectors.joining("-"));System.out.println(result);}}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.JoiningTest21-2-3-4Process finished with exitcode 0
多个参数
joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
方法接受一个字符串序列作为拼接符,并在拼接完成后添加传递的前缀和后缀。假如我们传递的分隔符为"-"
,前缀为"["
, 后缀为"]"
。那么输出结果为[1-2-3-4]
package com.sjh.test.java8;importjava.util.Arrays;importjava.util.List;importjava.util.stream.Collectors;publicclassJoiningTest3{publicstaticvoidmain(String[] args){List<String> list= Arrays.asList("1","2","3","4");String result= list.stream().collect(Collectors.joining("-", "[", "]"));System.out.println(result);}}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.JoiningTest3[1-2-3-4]Process finished with exitcode 0
如果流中的数据是字符串
使用joinning()
的三种重载方法来拼接字符串
package com.sjh.test.java8;importjava.util.Arrays;importjava.util.List;importjava.util.stream.Collectors;publicclassJoniningDemo1{publicstaticvoidmain(String[] args){List<String> list= Arrays.asList("JayLa","LayMan","KangKang","XinYI");String resultStr = list.stream().collect(Collectors.joining());System.out.println(resultStr);resultStr= list.stream().collect(Collectors.joining(","));System.out.println(resultStr);resultStr= list.stream().collect(Collectors.joining("-","[","]"));System.out.println(resultStr);}}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.JoniningDemo1JayLaLayManKangKangXinYIJayLa,LayMan,KangKang,XinYI[JayLa-LayMan-KangKang-XinYI]Process finished with exitcode 0
如果流中的数据是对象
首先,我们创建一个Person
类
package com.sjh.test.java8.bean;importjava.util.ArrayList;importjava.util.List;publicclassPerson{privateString name;privateintage;publicPerson(String name, intage){this.name = name;this.age = age;}publicString getName(){returnname;}publicintgetAge(){returnage;}publicstaticList<Person> getList() {List<Person> list= newArrayList<>();list.add(newPerson("JayLa", 23));list.add(newPerson("LayMan", 20));list.add(newPerson("KangKang", 25));list.add(newPerson("XinYi", 18));returnlist;}}
然后创建一个Person
对象流
package com.sjh.test.java8;importcom.sjh.test.java8.bean.Person;importjava.util.List;importjava.util.stream.Collectors;publicclassJoiningDemo2{publicstaticvoidmain(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);}}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.JoiningDemo2--Join person name--JayLaLayManKangKangXinYiJayLa|LayMan|KangKang|XinYi[JayLa-LayMan-KangKang-XinYi]--Join person age--2320251823|20|25|18[23-20-25-18]--Join person name-age--JayLa-23|LayMan-20|KangKang-25|XinYi-18[JayLa-23|LayMan-20|KangKang-25|XinYi-18]Process finished with exit code 0
end
如果觉得《Java 8 新特性|Collectors.joining() 详解》对你有帮助,请点赞、收藏,并留下你的观点哦!