Back Home


  • Stream's provide a scala-esque efficient and concise way to work with collections
  • Streams are designed to work with Java lambda expressions.

Obtaining a Stream From a Collection Stream Processing Phases Stream.filter() Stream.collect() Stream.min() and Stream.max() Stream.count() Stream.reduce()

Obtaining a Stream from a Collection

List<String> items = new ArrayList<String>();
Stream<String> stream =;

Stream Processing Phases

Once you have obtained a Stream instance from a Collection instance, you use that stream to process the elements in the collection.

Processing the elements in the stream happens in two steps / phases:

  • Configuration
    • Filters
    • mappings
  • Processing

Finish the rest of the tutorial here

List<String> names =newArrayList<>();
for(Student student : students){

There is nothing special about this code. This is a traditional Java external iterations example. Now, have a look at the below code. This line is doing exactly the same thing but we can't see any iteration logic here and hence it is called as internal iterations.

List<string> names =>name.startsWith("A"))


With a Return Value

  • CompletableFuture.supplyAsync() is your the means to call an async function and return the result (or error)

. It takes a Supplier<T> and returns CompletableFuture<T> where T is the type of the value obtained by calling the given supplier -

// Run a task specified by a Supplier object asynchronously
CompletableFuture<String> future = CompletableFuture.supplyAsync(new Supplier<String>() {
    public String get() {
        try {
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        return "Result of the asynchronous computation";
// Block and get the result of the Future
String result = future.get();

Using Java 8’s lambda expression to make the above code more concise

// Using Lambda Expression
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    try {
    } catch (InterruptedException e) {
        throw new IllegalStateException(e);
    return "Result of the asynchronous computation";

Async Error Handling

The library allows you to define a standard retry behavior

ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
RetryExecutor executor = new AsyncRetryExecutor(scheduler).
	withExponentialBackoff(500, 2).     //500ms times 2 after each retry
	withMaxDelay(10_000).               //10 seconds
	withUniformJitter().                //add between +/- 100 ms randomly

EA's Async Await

A very .net/javascript looking java async implementation

Also of interest

java8.txt · Last modified: 2018/06/29 07:53 by root
RSS - 200 © CrosswireDigitialMedia Ltd