Convert Java Stream to String

By Arvind Rai, October 18, 2020
On this page we will learn how to convert Java Stream to String. To convert stream into string, we can use following methods.
1. Collectors.joining
2. Stream.reduce
Now let us discuss them using examples.

Using Collectors.joining

The Collectors.joining returns a Collector that concatenates the input elements, separated by the specified delimiter, in encounter order.
public static Collector<CharSequence,?,String> joining(CharSequence delimiter) 
The Collector is passed as argument to collect method that performs a mutable reduction operation on the elements using a Collector.
<R,A> R collect(Collector<? super T,A,R> collector) 
Find the example.
UsingJoining.java
package com.concretepage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class UsingJoining {
  public static void main(String[] args) {
	   //example 1
	   String s1 = Stream.of("A", "B", "C", "D").collect(Collectors.joining());
	   System.out.println(s1);

	   //example 2	   
	   List<String> list = new ArrayList<>();
	   list.add("Gauri");
	   list.add("Lakshmi");
	   list.add("Saraswati");
	   String s2 = list.stream().collect(Collectors.joining(","));
	   System.out.println(s2);
	   
	   //example 3	   
	   List<City> cityList = Arrays.asList(new City(1, "Varanasi"), new City(2, "Prayag"), new City(3, "Ayodhya"));
	   String s3 = cityList.stream()
		   .map(c -> c.getName())
		   .collect(Collectors.joining("|"));
	   System.out.println(s3);
	   
	   //example 4	   
	   String s4 = cityList.stream()
		   .map(c -> new StringBuffer(c.getName()).append("-").append(c.getId()))
		   .collect(Collectors.joining("|"));
	   System.out.println(s4);	   
  }
}
class City {
  private int id;
  private String name;
  public City(int id, String name) {
	this.id = id;
	this.name = name;
  }
  //Sets and Gets
} 
Output
ABCD
Gauri,Lakshmi,Saraswati
Varanasi|Prayag|Ayodhya
Varanasi-1|Prayag-2|Ayodhya-3 

Using Stream.reduce

The Stream.reduce performs a reduction on the elements of this stream, using an associative accumulation function and returns Optional.
Optional<T> reduce(BinaryOperator<T> accumulator) 
Find the example to reduce a stream into a string.
UsingReduce.java
package com.concretepage;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class UsingReduce {
  public static void main(String[] args) {
	   //example 1
	   String s1 = Stream.of(1, 2, 3, 4).map(e -> e.toString()).reduce("", String::concat);
	   System.out.println(s1);

	   //example 2	   
	   List<String> list = new ArrayList<>();
	   list.add("Gauri");
	   list.add("Lakshmi");
	   list.add("Saraswati");
	   String s2 = list.stream().
		   reduce((x, y) -> new StringBuffer(x).append("-").append(y).toString()).get();
	   System.out.println(s2);
	   
	   //example 3	   
	   String s3 = Stream.of(new City(1, "Varanasi"), new City(2, "Prayag"), new City(3, "Ayodhya"))
		   .map(c -> c.getName())
		   .reduce((x, y) -> new StringBuffer(x).append("-").append(y).toString()).get();
	   System.out.println(s3);	   
  }
} 
Output
1234
Gauri-Lakshmi-Saraswati
Varanasi-Prayag-Ayodhya 

References

Stream
Collectors
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us