Example of different kind of streams in Java 8.

Streams can be created from various data sources, especially collections. Lists and Sets support new methods stream() and parallelStream() to either create a sequential or a parallel stream. Parallel streams are capable of operating on multiple threads and will be covered in a later section. Here we will check different ways to build streams from collections.

public class StreamTest {
     public static void main(String[] args){
         Stream stream = Stream.of("str1", "str2", "str3");
         stream.forEach(p -> System.out.println(p));
     }
}

In the above example we just use Stream.of() to create a stream from a bunch of object references.  We also can perform different kind of operation in such a Stream.

Stream.of("str1", "str2", "str3")
   .findFirst() 
   .ifPresent(System.out::println);

// The output of the above program is : str1

We can also create a Stream from an array of elements in the following way:

public class StreamTest {
	public static void main(String[] args) {
		Stream stream = Stream.of(new Integer[]{12,78,98,90} );
		stream.forEach(p -> System.out.println(p));
	}
}

Calling the method stream() on a list of objects returns a regular object stream.

 public static void main(String[] args){
         List list = new ArrayList();
         for(int i = 1; i< 10; i++){
             list.add(i);
         }
         Stream stream = list.stream();
         stream.forEach(p -> System.out.println(p));
 }

Besides regular object streams Java 8 ships with special kinds of streams for working with the primitive data types int, long and double. As you might have guessed it's IntStream, LongStream and DoubleStream. IntStreams can replace the regular for-loop utilizing IntStream.range():

IntStream.range(1, 4)
    .forEach(System.out::println);

// The output of the program is
1
2
3

All those primitive streams work just like regular object streams with the following differences: Primitive streams use specialized lambda expressions, e.g. IntFunction instead of Function or IntPredicate instead of Predicate. And primitive streams support the additional terminal aggregate operations sum() and average():

Arrays.stream(new int[] {1, 2, 3})
    .map(n -> 2 * n + 1)
    .average()
    .ifPresent(System.out::println);  // 5.0

Sometimes it's useful to transform a regular object stream to a primitive stream or vice versa. For that purpose object streams support the special mapping operations mapToInt(), mapToLong() and mapToDouble:

Stream.of("a1", "a2", "a3")
    .map(s -> s.substring(1))
    .mapToInt(Integer::parseInt)
    .max()
    .ifPresent(System.out::println);  // 3

Primitive streams can be transformed to object streams via mapToObj():

IntStream.range(1, 4) .mapToObj(i -> "a" + i) .forEach(System.out::println); 
// a1 // a2 // a3

Here's a combined example: the stream of doubles is first mapped to an int stream and than mapped to an object stream of strings:

Stream.of(1.0, 2.0, 3.0) 
 .mapToInt(Double::intValue) 
 .mapToObj(i -> "a" + i) .forEach(System.out::println);

 // a1 // a2 // a3

 

Java-8 12 Stream 12

FOLLOW US ON LinkedIn



Explore Tutu'rself