In this article, we will learn the use of joining()
method in Java 8 Collectors API with the help of examples.
Table of contents
Definition
The joining()
method of the Collectors class is used to join a series of Characters or Strings into a single String object. The Characters or Strings will be concatenated in the same order as it is provided.
Overloaded methods
The joining()
method is overloaded in 3 ways:
- static Collector joining()
- static Collector joining(CharSequence delimiter)
- static Collector joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
static Collector joining()
This method returns a Collector that concatenates or joins the input elements into a String. The joining()
method will maintain the order of the input elements. This is the simplest form of joining.
Example 1: We will join the names from a list of Persons
Person.java package com.getinputs; public class Person { String name; int age; Person(String name, int age) { this.name=name; this.age=age; } public String getName() { return name; } public int getAge() { return age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
Joining.java package com.getinputs; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class Joining { static List<Person> createPeople() { List<Person> list = new ArrayList<>(); list.add(new Person("Suraj ", 32)); list.add(new Person("Iqbal ", 34)); list.add(new Person("Suchit ", 25)); list.add(new Person("Amar ", 30)); list.add(new Person("Kartik ", 32)); return list; } public static void main(String[] args) { List<Person> list = createPeople(); String str = list.stream().map(person -> person.getName()).collect(Collectors.joining()); System.out.println(str); } } Output : Suraj Iqbal Suchit Amar Kartik
Example 2: We will join the array of Characters to create a String
JoiningChars .java package com.getinputs; import java.util.stream.Collectors; import java.util.stream.Stream; public class JoiningChars { public static void main(String[] args) { char[] chars = {'g','e','t','i','n','p','u','t','s','.','c','o','m'}; String str = Stream.of(chars) .map(String::valueOf) .collect(Collectors.joining()); System.out.println(str); } } Output : getinputs.com
static Collector joining(CharSequence delimiter)
This method returns a Collector that concatenates or joins the input elements into a String using a delimiter. The joining()
method will maintain the order of the input elements.
Note* delimiter is a symbol or char sequence to join the elements.
Example 1: We will join the names from a list of Persons using a comma delimiter.
Joining.java package com.getinputs; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class Joining { static List<Person> createPeople() { List<Person> list = new ArrayList<>(); list.add(new Person("Suraj", 32)); list.add(new Person("Iqbal", 34)); list.add(new Person("Suchit", 25)); list.add(new Person("Amar", 30)); list.add(new Person("Kartik", 32)); return list; } public static void main(String[] args) { List<Person> list = createPeople(); String str = list.stream().map(person -> person.getName()).collect(Collectors.joining(",")); System.out.println(str); } } Output: Suraj,Iqbal,Suchit,Amar,Kartik
Example 2: We will join the list of String with a comma delimiter
JoiningStrings .java package com.getinputs; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class JoiningStrings { public static void main(String[] args) { List<String> list = Arrays.asList("hello", "getinputs", "com"); String str = list.stream().collect(Collectors.joining(",")); System.out.println(str); } } Output: hello,getinputs,com
static Collector joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
This method returns a Collector that concatenates or joins the input elements into a String using a delimiter along with a prefix and a suffix.
- The delimiter is a symbol to join the elements
- A prefix is a symbol that we will append at the start of the first element
- A suffix is a symbol that we will append after the last element of the string
Example 1: We will join the names from a list of Persons using a comma delimiter and add square brackets at the start and end of the String.
Joining .java package com.getinputs; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class Joining { static List<Person> createPeople() { List<Person> list = new ArrayList<>(); list.add(new Person("Suraj", 32)); list.add(new Person("Iqbal", 34)); list.add(new Person("Suchit", 25)); list.add(new Person("Amar", 30)); list.add(new Person("Kartik", 32)); return list; } public static void main(String[] args) { List<Person> list = createPeople(); String str = list.stream().map(person -> person.getName()).collect(Collectors.joining(",", "[", "]")); System.out.println(str); } } Output : [Suraj,Iqbal,Suchit,Amar,Kartik]
Example 2: We will join the list of String using a comma delimiter and add braces at the start and end of the String
JoiningStrings .java package com.getinputs; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class JoiningStrings { public static void main(String[] args) { List<String> list = Arrays.asList("hello", "getinputs", "com"); String str = list.stream().collect(Collectors.joining(",", "{", "}")); System.out.println(str); } } Output : {hello,getinputs,com}
I hope you found this article interesting and valuable. If you are having any concerns or questions about this article please comment below.