Showing posts with label Stream. Show all posts
Showing posts with label Stream. Show all posts

Tuesday, April 14, 2020

Java 8 flatMap Examples - Stream flatmap convert Stream<List<List<String>>> into Stream<String>

1. Introduction


In this article, We'll learn about java 8 new Stream API flatMap() method. When to use it and how to use it.

flatMap() method is used to convert or flatten Stream of collections into Stream of collection values by removing the collection.

Removing collection such as List or Set from Stream is called flattening.

FlatMap() is part of Stream Intermediate Operations in Java 8.

Java 8, Stream can hold any type of collections and can be converted into Stream<T> as below.

Stream<List<List<String>>> --> apply flatMap() logic --> Stream<String>
Stream<Set<Set<String>>> --> apply flatMap() logic --> Stream<String>
Stream<List<String>>> --> apply flatMap() logic --> Stream<String>
Stream<List<Object>>> --> apply flatMap() logic --> Stream<Object>


Wednesday, September 11, 2019

Java 8: How to Get the Last Element of a Stream in Java?

$type=blogging

1. Introduction

[post_ads]

in this tutorial, We will be looking at different ways to get the last element of a Stream in Java 8.

When we work with Stream then we have to go through one by one element of Stream. But, if we want to get directly last value and ignore the remaining preceding elements.

here are the possible ways to retrieve only the last value from the stream.

2. Using reduce() method

[lock]
reduce() method is part of the Stream API and we have to pass the lambda expression with two arguments. [/lock]

Syntax:


Optional reduce​(BinaryOperator accumulator)


Tuesday, August 20, 2019

Java 8 Streams filter() - Working Examples

1. Overview


In this tutorial, We'll learn about the new Java 8 Streams API filter() method. This filter method is the one mostly used in streams. Streams build the operations sequentially and parallel.

filter() method takes Predicate which holds a condition. If the condition is true then this element is passed to the next operation in the stream.


We will discuss with example programs so that you can understand clearly.

First, will talk about syntax, examples and how it works internally.

Friday, August 16, 2019

Java 8 StreamSupport Examples

1. Overview


In this tutorial, We'll be learning Java 8 StreamSupport class methods with example programs. Let us see its syntax and how to convert iterable to Stream using StreamSupport.stream() method. This class is part of java.util.stream package.

Core methods of StreamSupport class are stream(), intStream(), doubleStream(), longStream().

Thursday, July 25, 2019

Java 8 Stream allMatch() Method

Stream allMatch() Method 


In this tutorial, We will see the example program on Java 8 Stream allMatch() method. allMatch() method checks whether all elements of Stream is matched to the given predicate then it returns true. Otherwise returns false.


This is called Terminal Operation because this is executed at the end of the stream. That means Stream is closed after the execution of allMatch() method.


Java 8 Stream allMatch()