Core Java

Java 8 – Convert IntStream to String

A quick guide to convert IntStream to String in java 8 streams.

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.

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.

GitHub

IntStream examples

Published on Java Code Geeks with permission by Venkatesh Nukala, partner at our JCG program. See the original article here: Java 8 – Convert IntStream to String

Opinions expressed by Java Code Geeks contributors are their own.

Venkatesh Nukala

Venkatesh Nukala is a Software Engineer working for Online Payments Industry Leading company. In my free time, I would love to spend time with family and write articles on technical blogs. More on JavaProgramTo.com
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
angios
2 years ago

Great article…..

Back to top button