Pages

Friday, November 26, 2021

Java 8 - Convert IntStream to String

1. Overview

In this tutorial, We'll learn how to convert IntStream into String value in java 8.

In previous tutorials, we have seen how to convert IntStream to List and IntStream to Array using stream API methods.

Java 8 - Convert IntStream to String



2. Java 8 Convert IntStream to String using mapToObj()


First, Create the IntStream instance using IntStream.of() method by passing 10, 20, 30 values. After that we need to convert the IntStream into the Stream<String> values using mapToObj() method and next use the collect reduction operation to join the numbers.

mapToObj() is a intermediate operation and collect() is a terminal operation.

Example 1
package com.javaprogramto.java8.intstream.tostring;

import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class IntStreamToStringExample1 {

	public static void main(String[] args) {

		IntStream nums = IntStream.of(10, 20, 30);

		Stream<String> stream = nums.mapToObj(i -> String.valueOf(i));

		// string without any separator
		String string1 = stream.collect(Collectors.joining());
		System.out.println("String 1 : " + string1);

		// string without any separator with - delimiter
		IntStream nums2 = IntStream.of(10, 20, 30);
		String string2 = nums2.mapToObj(i -> String.valueOf(i)).collect(Collectors.joining("-"));
		System.out.println("String 2 : " + string2);

		// string without any separator - with delimiter, prefix and suffix
		IntStream nums3 = IntStream.of(10, 20, 30);
		String string3 = nums3.mapToObj(i -> String.valueOf(i)).collect(Collectors.joining("-", "{", "}"));
		System.out.println("String 3 : " + string3);
	}
}

Output
String 1 : 102030
String 2 : 10-20-30
String 3 : {10-20-30}

Here the output string can be generated in different formats using Collectors.joining() method.


3. Java 8 Convert IntStream to String Using boxed()


Next, use boxed() method on IntStream and map() method from stream api.

Example 2
package com.javaprogramto.java8.intstream.tostring;

import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class IntStreamToStringExample2 {

	public static void main(String[] args) {

		IntStream nums = IntStream.of(10, 20, 30);

		Stream<String> stream = nums.boxed().map(i -> String.valueOf(i));

		// string without any separator
		String string1 = stream.collect(Collectors.joining());
		System.out.println("String 1 : " + string1);

		// string without any separator with - delimiter
		IntStream nums2 = IntStream.of(10, 20, 30);
		String string2 = nums2.boxed().map(i -> String.valueOf(i)).collect(Collectors.joining("-"));
		System.out.println("String 2 : " + string2);

		// string without any separator - with delimiter, prefix and suffix
		IntStream nums3 = IntStream.of(10, 20, 30);
		String string3 = nums3.boxed().map(i -> String.valueOf(i)).collect(Collectors.joining("-", "{", "}"));
		System.out.println("String 3 : " + string3);
	}
}

Output

String 1 : 102030
String 2 : 10-20-30
String 3 : {10-20-30}

4. Conclusion


In this article, we've seen how to convert IntStream to String in java 8.
This is a way of converting a stream of integers into String with optional delimiter, prefix and suffix values.



No comments:

Post a Comment

Please do not add any spam links in the comments section.