Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Streams with primitives data types and corresponding wrappers

Tags:

While playing around with Java8's Streams-API, I stumbled over the following:

To convert an array of primitive wrapper classe objects into a Stream I just have to call Stream.of(array). But to convert an array of primitive data types, I have to call .of(array) from the corresponding wrapper (class) stream class (<-- that sounds silly).

An example:

final Integer[] integers = {1, 2, 3}; final int[]     ints     = {1, 2, 3};   Stream.of(integers).forEach(System.out::println); //That works just fine  Stream.of(ints).forEach(System.out::println);     //That doesn't  IntStream.of(ints).forEach(System.out::println);  //Have to use IntStream instead 


My question(s): Why is this? Does this correlate to e.g. the behaviour of Arrays.asList() which also just works for wrapper class arrays?

like image 934
ifloop Avatar asked Apr 11 '14 08:04

ifloop


People also ask

What is corresponding wrapper class for primitive data type?

Explanation: The Wrapper class for the int primitive data type is Integer.

Can you use streams with primitives Java?

Streams primarily work with collections of objects and not primitive types. Fortunately, to provide a way to work with the three most used primitive types – int, long and double – the standard library includes three primitive-specialized implementations: IntStream, LongStream, and DoubleStream.

What is the difference between wrapper class and primitive data type?

The difference between wrapper class and primitive type in Java is that wrapper class is used to convert a primitive type to an object and object back to a primitive type while a primitive type is a predefined data type provided by the Java programming language.


1 Answers

Java 8 stream framework has a generic Stream<T> for objects as elements, and three primitive streams IntStream, LongStream, DoubleStream for the main three primitives. If you work with primitives, use one of those latter, in your case IntStream.

See the picture:

enter image description here

What lies behind is that:

  1. Java generics cannot work with primitive types: it is possible to have only List<Integer> and Stream<Integer>, but not List<int> and Stream<int>

  2. When the Java Collections framework was introduced, it was introduced only for classes, so if you want to have a List of ints, you have to wrap them (each single element!) to Integers. This is costly!

  3. When the Java Streams framework was introduced, they decided to get around this overhead and in parallel with the "class-oriented" streams (using the generics mechanism), they introduced three extra sets of all the library functions, specifically designed for the most important primitive types: int, long, double.

BTW, they did the same with the predefined functional interfaces in the java.util.function package, so you have Predicate, IntPredicate, DoublePredicate, and LongPredicate.

And see also a marvelous explanation here: https://stackoverflow.com/a/22919112/2886891

like image 187
Honza Zidek Avatar answered Sep 19 '22 06:09

Honza Zidek