DEV Community

Isurumax26
Isurumax26

Posted on

Stream API Java

As we all know java introduces set of new features in its version 8.
Stream API is one of that.
First we will see how to create an stream :

Stream Creation

  1. Collections to stream Let's see how we can convert a list into a stream.In this way we can create collection, set to a stream

List<String> cities = Arrays.asList("London", "Tokyo", "Manila");
Stream<String> cityStream = cities.stream();

In the following code snippet you can see how we can convert an array into a stream.
String[] foods = {"Pizza", "Egg", "Chicken"};
Stream<String> foodsStream = Arrays.stream(foods);

We can also create an stream out of a part of an array

Stream<String> foodsStream = Arrays.stream(foods, 1, 3);

  1. Map to a Stream In this snippet I'm going to show you how we can convert a java collection type map in to a stream. In this code first i convert the map to a entryset and then to a stream

Map<String, Integer> hm = new HashMap<>();
Stream<Map.Entry<String, Integer>> entryStream = hm.entrySet().stream();

One of a best practise when using stream instance is that if you want to return null object. Just return a empty stream (This is a good for other collections as well. Such as list, set, map)
Stream.empty()

we often use empty() method upon creation to avoid returning null for streams with no element.

public Stream<String> streamOf(List<String> list) {
return list == null || list.isEmpty() ? Stream.empty() : list.stream();
}

  1. Using builder method We can create a stream using this method too. Remember when you are using this method you have to specify the type too. If not it will return an instances of Stream.

Stream<String> streamBuilder =
Stream.<String>builder().add("a").add("b").add("c").build();

but according to my knowledge we don't often use this type of creation.

  1. Streams of primitives we can create streams out of int, long and double. Main advantage of this is we can avoid unnecessary usage of auto unboxing. It will increase the performances of your code.

IntStream intStream = IntStream.range(1, 3);
LongStream longStream = LongStream.rangeClosed(1, 3);

In the first method it doesn't include the value of second parameter(one value less than the second parameter)

But in the second method it include that value as well.

Remember that stream is literally a stream. If you get to a point you can't move back likewise after you created a stream you can't again reuse that stream.

Optional<String> anyElement = stream.findAny();
Optional<String> firstElement = stream.findFirst();

It will throw an IlleagalStateException which is a Runtime exception.

Conclusion
Stream API is one of the powerful feature in java 8. You have to remember that all the methods which i have used are running in a single thread. But stream API allows you create parallels streams in multiple threads as well. We will check all those in future series.This is just a beginning of a series let's dig deep in to the streams in the following articles. will meet u soon -:D

Let me know your suggestion and improvement for this series.

Top comments (0)