Core Java

Java 8 – Convert IntStream To List and Other

A quick guide on how to convert IntStream to List in java 8.

1. Overview

In this tutorial, We’ll learn how to convert IntStream to List in java 8 and java 16 above versions.

IntStream is used to create the infinite streams in java 8 and it has the method to convert it to array using
toArray() method.

But there are no other methods to directly convert it to List or set.

Conversion of IntStream to List can be done in two ways.

2. Java 8 – IntStream to List or Set

In java 8 API, IntStream class has boxed() method. boxed() method converts the primitive int values into a stream of integer objects.

Once we get the Stream<Integer> instance then we can convert it to the List or Set or Map or any collection.

Example 1

package com.javaprogramto.java8.intstream.toList;

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

public class IntStreamToListExample {

	public static void main(String[] args) {

		IntStream evenIntStream = IntStream.iterate(0, i -> i + 2);

		Stream<Integer> stream = evenIntStream.limit(100).boxed();

		List<Integer> list = stream.collect(Collectors.toList());

		System.out.println("list size : " + list.size());

		IntStream evenIntStream2 = IntStream.iterate(0, i -> i + 2);
		
		Stream<Integer> stream2 = evenIntStream2.limit(100).boxed();

		List<Integer> linkedList = stream2.collect(Collectors.toCollection(LinkedList::new));

		System.out.println("linkedList size : " + linkedList.size());

	}
}

Output

list size : 100
linkedList size : 100

3. Java 16 – IntStream to List or Set

JDK 16 is added with the toList() method in Stream api. but the returned List is an unmodifiable list.

Example 2

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

public class IntStreamToListExample2 {

	public static void main(String[] args) {

		IntStream evenIntStream = IntStream.iterate(0, i -> i + 2);

		List<Integer> list = evenIntStream.limit(100).boxed().toList();

		System.out.println("list size : " + list.size());
	}
}

Output:

list size : 100

4. Conclusion

In this article, We’ve seen how to convert InstStream to Collection object in Java 8 and 16 versions.

GitHub

Stream.toList()

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 List and Other

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.

0 Comments
Inline Feedbacks
View all comments
Back to top button