Boxed Streams in Java

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

1. What is a Boxed Stream?

Java Stream API has been designed to work with objects, similar to Collections API. Streams do not treat the primitive types the same as objects.

In Stream API, a stream of primitives can be represented by the following 3 classes:

  • IntStream
  • LongStream
  • DoubleStream

To convert from a stream of primitives to a stream of objects, these classes provide boxed() method that returns a Stream consisting of the elements of the given stream, each boxed to an object of the corresponding wrapper class.

Stream<Integer> stream = IntStream.of(1, 2, 3, 4, 5).boxed();

Stream<Long> stream1 = LongStream.of(1, 2, 3, 4, 5).boxed();

Stream<Double> stream2 = DoubleStream.of(1.0, 2.0, 3.0, 4.0, 5.0).boxed();

2. Need of Boxed Streams

Without boxing the stream items, we cannot perform the regular stream operations on them. For example, we cannot collect the int values to a list, directly.

//Compilation issue
/*List<Integer> list = IntStream.of(1,2,3,4,5)
    .collect(Collectors.toList());*/

To make the above collecting process work, we must box the stream items first.

//Works fine
List<Integer> list = IntStream.of(1,2,3,4,5)
    .boxed()
    .collect(Collectors.toList());

Happy Learning !!

Sourcecode on Github

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

3 thoughts on “Boxed Streams in Java”

  1. 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>> ?

    Reply
    • 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

      Reply
    •  
       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());
      
      Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.