Java 8 Collectors: joining() Example

By Arvind Rai, September 03, 2016
On this page we will provide java 8 Collectors joining() example. joining() method returns Collector instance. It concatenates the input elements in encounter order. We can pass delimiter, prefix and suffix too in element concatenation. Find the example.

joining() Method Description

Suppose we the elements A,B,C,D that will be collected in following ways.
1.
joining()
 
It returns the Collector that is used to concatenate the elements with no delimiter. For our given input elements, the output string will be ABCD.
2.
joining(CharSequence delimiter)
 
It returns the Collector that is used to concatenate the elements with given delimiter. For our given input elements and separator (-), the output string will be A-B-C-D.
3.
joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
 
It returns the Collector that is used to concatenate the elements with given delimiter, prefix and suffix . For our given input elements, separator(-), prefix([) and suffix(])the output string will be [A-B-C-D].

Collectors.joining() with List of String


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);        
    }		
} 
Output
RamShyamShivMahesh
Ram,Shyam,Shiv,Mahesh
[Ram-Shyam-Shiv-Mahesh] 

Collectors.joining() with List of Objects


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;
	}
} 
Output
--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] 

Reference

Java 8 Doc: Collectors
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us