Java8 Collectors.joining() 详解

扫码关注公众号:Java 技术驿站

发送:vip
将链接复制到本浏览器,永久解锁本站全部文章

【公众号:Java 技术驿站】 【加作者微信交流技术,拉技术群】
免费领取10G资料包与项目实战视频资料

本章节我们来详细讲讲 Java 8 流 ( stream ) 收集器 ( Collectors ) 中的 joining() 方法。该方法会返回一个 Collectors 实例,方便在流收集器上的链式操作。

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

joinning() 方法定义

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

joining()

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

JoiningExample.java

    package cn.twle.util.stream;
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    public class JoiningExample {
        public static void main(String[] args) {
           List<String> list = Arrays.asList("A","B","C","D");
           String result=  list.stream().collect(Collectors.joining());
           System.out.println(result);
        }
    }

输出结果为 ABCD

joining(CharSequence delimiter)

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

JoiningExample.java

    package cn.twle.util.stream;
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    public class JoiningExample {
        public static void main(String[] args) {
           List<String> list = Arrays.asList("A","B","C","D");
           String result=  list.stream().collect(Collectors.joining("-"));
           System.out.println(result);
        }
    }

运行结果为 A-B-C-D

joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

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

JoiningExample.java

    package cn.twle.util.stream;
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    public class JoiningExample {
        public static void main(String[] args) {
           List<String> list = Arrays.asList("A","B","C","D","[","]");
           String result=  list.stream().collect(Collectors.joining("-"));
           System.out.println(result);
        }
    }

运行结果为 [A-B-C-D]

范例

范例 1 : 如果流中的数据是字符串

下面的代码演示了如何使用 joinning() 的三种重载方法来拼接字符串

JoiningExampleWithListOfString.java

    package cn.twle.util.stream;
    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);        
        }       
    } 

运行结果为

    RamShyamShivMahesh
    Ram,Shyam,Shiv,Mahesh
    [Ram-Shyam-Shiv-Mahesh] 

范例 2: 如果流中的数据是对象

如果流中的数据是对象,下面的代码演示了如何拼接它们。

首先,我们创建一个 Person

Person.java

    package cn.twle.util.stream;
    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;
        }
    } 

然后创建一个 Person 对象流

JoiningExampleWithListOfObject.java

    package cn.twle.util.stream;
    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);        
        }       
    } 

运行结果为

    --Join person name--
    RamShyamShivMahesh
    Ram|Shyam|Shiv|Mahesh
    [Ram-Shyam-Shiv-Mahesh]
    --Join person age--
    23202530
    23|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] 

来源:https://www.twle.cn

赞(0) 打赏
版权归原创作者所有,任何形式的转载请联系博主:daming_90:Java 技术驿站 » Java8 Collectors.joining() 详解

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏