HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java 8 / Java Boxed Stream Example

Java Boxed Stream Example

Java a boxed stream is a stream of the wrapper class instances to simulate a stream of primitives.

1. What is a Boxed Stream?

In Java 8, if we want to convert a stream of objects to a collection then we can use one of the static methods in the Collectors class.

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

However, the same process doesn’t work on streams of primitives.

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

To convert a stream of primitives, we must first box the elements in their wrapper classes and then collect the wrapped objects in a collection. This type of stream is called boxed stream.

Let’s look at a few examples of boxed streams in Java.

2. IntStream (Stream of int)

Example to convert int stream to List of Integers.

//Get the collection and later convert to stream to process elements
List<Integer> ints = IntStream.of(1,2,3,4,5)
				.boxed()
				.collect(Collectors.toList());
		
System.out.println(ints);

//Stream operations directly
Optional<Integer> max = IntStream.of(1,2,3,4,5)
				.boxed()
				.max(Integer::compareTo);

System.out.println(max);

Program Output:

[1, 2, 3, 4, 5]
5

3. LongStream (Stream of long)

Example to convert long stream to List of Longs.

List<Long> longs = LongStream.of(1l,2l,3l,4l,5l)
				.boxed()
				.collect(Collectors.toList());
		
System.out.println(longs);

Output:

[1, 2, 3, 4, 5]

4. DoubleStream (Stream of double)

Example to convert double stream to List of Doubles.

List<Double> doubles = DoubleStream.of(1d,2d,3d,4d,5d)
				.boxed()
				.collect(Collectors.toList());
		
System.out.println(doubles);

Output:

[1.0, 2.0, 3.0, 4.0, 5.0]

Drop me your questions in comments section related to boxed streams or stream of primitives in Java Stream API.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

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

Feedback, Discussion and Comments

  1. Tim

    May 3, 2019

    int[] answer = new int[] { 3, 2 }
    List<Integer> expected = Arrays.stream(answer).boxed().collect(Collectors.toList());
    
    Object[] objArrayOfIntArrays = new Object[] {
                    new int[] { 1, 2 }, // add 2
                    new int[] { 1, 2 }, // add 2
                    new int[] { 3, 2 }, // query freq 2
                    new int[] { 2, 2 }, // delete 2
                    new int[] { 3, 2 }, // query freq 2
                }
    

    My question is, is there a way to turn objArrayOfIntArrays to List<List<Integer>> ?

    • Praveen

      May 22, 2019

      Hi Tim,

       
      Object[] objArrayOfIntArrays1 = new Object[] { new int[] { 1, 2 }, // add 2
      		new int[] { 1, 2 }, // add 2
      		new int[] { 3, 2 }, // query freq 2
      		new int[] { 2, 2 }, // delete 2
      		new int[] { 3, 2 }, // query freq 2
      };
      
      List<List<Integer>> primitiveList = Arrays
      					.stream(objArrayOfIntArrays1)
      					.map(q -> {
      						return Arrays.stream((int[]) q)
      						        .boxed()
      							.collect(Collectors.toList());
      						}).collect(Collectors.toList());
      
      

      Hope it Helps,
      Praveen

    • Praveen

      May 22, 2019

       
       Object[] objArrayOfIntArrays1 = new Object[] {
                   new int[] { 1, 2 }, // add 2
                   new int[] { 1, 2 }, // add 2
                   new int[] { 3, 2 }, // query freq 2
                   new int[] { 2, 2 }, // delete 2
                   new int[] { 3, 2 }, // query freq 2
               };
        	   
        	 List<List<Integer>> primitiveList=Arrays.stream(objArrayOfIntArrays1)
      		       .map(q->{return Arrays.stream((int[]) q).boxed().collect(Collectors.toList());}).collect(Collectors.toList());
      
Comments are closed on this article!

Search Tutorials

Java 8 Tutorial

  • Java 8 Features
  • Java 8 forEach
  • Java 8 Stream
  • Java 8 Boxed Stream
  • Java 8 Lambda Expression
  • Java 8 Functional Interface
  • Java 8 Method Reference
  • Java 8 Default Method
  • Java 8 Optional
  • Java 8 Predicate
  • Java 8 Regex as 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 Difference Between Dates
  • Java 8 Join Array
  • Java 8 Join String
  • Java 8 Exact Arithmetic
  • Java 8 Comparator
  • Java 8 Base64
  • Java 8 SecureRandom
  • Internal vs External Iteration

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

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 © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces