Home » Java » Core Java » Java 8 – Convert IntStream To List and Other

About Venkatesh Nukala

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

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.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
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