June 2018
Beginner
722 pages
18h 47m
English
The Stream<T> of(T... values) method accepts varargs, or an array of values, and creates a Stream object with the provided values as the stream elements:
Stream.of("1 ", 2).forEach(System.out::print); //prints: 1 2 //Stream<String> stringStream = Stream.of("1 ", 2); //compile error String[] strings = {"1 ", "2"}; Stream.of(strings).forEach(System.out::print); //prints: 1 2
Notice that in the first line of the preceding code, a Stream object accepts elements of different types if there is no type specified in the generics of the Stream reference declaration. In the next line, the generics define the type of the Stream object as String, and the same mix of element types generates a compile error. Generics definitely help programmers ...
Read now
Unlock full access