Java

Java 8 streams examples that you should use

5
(1)

If you do programming as part of your day to day life, you will encounter loops multiple times a day. Java 8 stream APIs are pretty handy for various activities. Let me through in some useful examples that you can use in your day to day life.

For downloading complete example, you can visit the following github repository link.

https://github.com/anilpank/java8examples/blob/master/src/org/anon/ListExample.java

Let us first just define a list and add some elements in it.

List<String> list = new ArrayList<>();
list.add("John");
list.add("Mary");
list.add("Steve");
list.add("Abraham");
list.add("Bill");
list.add("Donald");
list.add("Joe");
list.add("Angela");

Now print all elements of the list.

 list.stream().forEach(val -> System.out.print(val+","));

Print all elements starting with A.

list.stream().filter(val -> val.startsWith("A")).forEach(val -> System.out.print(val+","));

Find all names matching a given condition.

System.out.println(list.stream().allMatch(val-> val.contains("m")));

Find any names matching a given condition.

System.out.println(list.stream().anyMatch(val -> val.startsWith("M")));

Collect the elements to a new list after performing a specific filter.

list.stream().filter(val -> val.startsWith("P")).collect(Collectors.toList()).forEach(v -> System.out.print(v+","));

Find count of all items.

System.out.println(list.stream().count());

Find count of all distinct items.

System.out.println(list.stream().distinct().count());

Find first element of the list.

System.out.println(list.stream().findFirst().get());

Flatten a 2D Array into a 1D Array.

String[][] multiArray = {{"1","2","3"},{"4","5","6"}};

Limit to only x number of elements of the list.

list.stream().limit(2).forEach(System.out::print);

Create a new stream from the current stream of the list.

list.stream().map(v-> v.length()*3).forEach(System.out::print);

Create an Integer stream from a list of Strings.

list.stream().mapToInt(v -> v.length()).forEach(w->System.out.print(w*w));

Find max element of the list based on your custom inline comparator.

System.out.println(list.stream().max((a,b) -> a.length() - b.length()).get());

Find min element of the list based on your custom comparator.

System.out.println(list.stream().min((a,b) -> a.compareTo(b)).get());

Find if none of the elements in the list matches a given condition.

System.out.println(list.stream().noneMatch(a-> a.equals("Anila")));

Skip the first N elements and then perform something.

list.stream().skip(1).forEach(System.out::print);

Sort the list based on your custom comparator logic.

list.stream().sorted((a,b) -> a.length()-b.length()).forEach(System.out::print);
list.stream().sorted((a,b) -> a.compareTo(b)).forEach(System.out::print);

 

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Tagged , ,
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments