HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Convert IntStream to collection or array

By Lokesh Gupta | Filed Under: Java 8

Java 8‘s Collectors class provides many static methods to collect objects from a stream and store them into collection. But these method does not work with streams of primitives.

//It works perfect !!
List<String> strings = Stream.of("how", "to", "do", "in", "java")
    				.collect(Collectors.toList());

//Compilation Error !!
IntStream.of(1,2,3,4,5).collect(Collectors.toList());

To convert primitives stream to collection, you can adapt on of following two ways.

1. IntStream to collection – boxed streams

Using boxed() method of IntStream, LongStream or DoubleStream e.g. IntStream.boxed(), you can get stream of wrapper objects which can be collected by Collectors methods.

List<Integer> ints = IntStream.of(1,2,3,4,5)
				.boxed()
				.collect(Collectors.toList());
		
System.out.println(ints);

Output:

[1, 2, 3, 4, 5]

2. IntStream to list – map int to Integer

Another way is to manually to the boxing using IntStream.mapToObj(), IntStream.mapToLong() or IntStream.mapToDouble() methods. There are other similar methods in other stream classes, which you can use.

List<Integer> ints = IntStream.of(1,2,3,4,5)
		    .mapToObj(Integer::valueOf)
		    .collect(Collectors.toList());
		
System.out.println(ints);

Output:

[1, 2, 3, 4, 5]

3. Convert IntStream to array

It is also useful to know how to convert primitive stream to array. Use IntStream.toArray() method to convert from int stream to array.

int[] intArray = IntStream.of(1, 2, 3, 4, 5).toArray();

System.out.println(Arrays.toString(intArray));

Output:

[1, 2, 3, 4, 5]

Similarly, use toArray() function of LongStream or DoubleStream as well.

Drop me your questions in comments section.

Happy Learning !!

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

1
Leave a Reply

This comment form is under antispam protection
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
This comment form is under antispam protection
  Subscribe  
newest oldest most voted
Notify of
Priya

You made the concept very easy to understand. Thanks for sharing.

Vote Up0Vote Down  Reply
2 years ago

Search Tutorials

Java 8 Tutorial

  • Java 8 – Introduction
  • Java 8 – forEach
  • Java 8 – Stream
  • Java 8 – Boxed Stream
  • Java 8 – Lambda Expression
  • Java 8 – Functional Interface
  • Java 8 – Method References
  • Java 8 – Default Method
  • Java 8 – Optionals
  • Java 8 – Predicate
  • Java 8 – Date Time
  • Java 8 – Iterate Directory
  • Java 8 – Read File
  • Java 8 – Write to File
  • Java 8 – WatchService
  • Java 8 – String to Date
  • Java 8 – Join Array
  • Java 8 – Base64
  • Java 8 – Exact Arithmetic
  • Java 8 – Comparator
  • Java 8 – Regex as Predicate
  • Java 8 – Join String
  • Java 8 – Difference Between Dates
  • Internal vs. External Iteration
  • Java 8- SecureRandom

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz