java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java8新特性Collectors.joining()

Java8新特性之Collectors.joining()实例详解

作者:Java小技巧

在项目中我们常常要对list集合的数据做一些字符串拼接/处理等相关操作,下面这篇文章主要给大家介绍了关于Java8新特性之Collectors.joining()的相关资料,需要的朋友可以参考下

方法定义

Java 8 流 ( stream ) 收集器 ( Collectors ) 中的 joining() 方法会返回一个 Collectors 实例,方便在流收集器上的链式操作。

Collectors.joining() 方法以遭遇元素的顺序拼接元素。我们可以传递可选的拼接字符串、前缀和后缀。

假设我们的流中有四个元素 ["A","B","C","D"],那么我们就可以按照以下方式来收集它们。

无参方法

joinning() 无参数方法会返回一个 Collectors 实例,并且以空字符串 ( "" ) 来拼接收集到的所有元素

package com.sjh.test.java8;
 
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
public class JoiningTest1 {
    public static void main(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.JoiningTest1
1234
 
Process finished with exit code 0

单个参数

joining(CharSequence delimiter) 接受一个参数字符串序列作为拼接符,并返回一个 Collectors 实例。假如我们传递的拼接符为 "-" 。那么输出结果为 1-2-3-4

package com.sjh.test.java8;
 
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
public class JoiningTest2 {
    public static void main(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.JoiningTest2
1-2-3-4
 
Process finished with exit code 0

多个参数

joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) 方法接受一个字符串序列作为拼接符,并在拼接完成后添加传递的前缀和后缀。假如我们传递的分隔符为 "-",前缀为 "[" , 后缀为 "]" 。那么输出结果为 [1-2-3-4]

package com.sjh.test.java8;
 
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
public class JoiningTest3 {
    public static void main(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 exit code 0

如果流中的数据是字符串

使用 joinning() 的三种重载方法来拼接字符串

package com.sjh.test.java8;
 
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
public class JoniningDemo1 {
 
    public static void main(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.JoniningDemo1
JayLaLayManKangKangXinYI
JayLa,LayMan,KangKang,XinYI
[JayLa-LayMan-KangKang-XinYI]
 
Process finished with exit code 0

如果流中的数据是对象

首先,我们创建一个 Person 类

package com.sjh.test.java8.bean;
 
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("JayLa", 23));
        list.add(new Person("LayMan", 20));
        list.add(new Person("KangKang", 25));
        list.add(new Person("XinYi", 18));
        return list;
    }
}

然后创建一个 Person 对象流

package com.sjh.test.java8;
 
import com.sjh.test.java8.bean.Person;
 
import java.util.List;
import java.util.stream.Collectors;
 
public class JoiningDemo2 {
    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);
    }
}

运行结果:

/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.JoiningDemo2
 
--Join person name--
JayLaLayManKangKangXinYi
JayLa|LayMan|KangKang|XinYi
[JayLa-LayMan-KangKang-XinYi]
 
 --Join person age--
23202518
23|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

总结

到此这篇关于Java8新特性之Collectors.joining()实例详解的文章就介绍到这了,更多相关Java8新特性Collectors.joining()内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文