$show=/label

Java 8 and Infinite Streams - How To Create Infinite Streams

SHARE:

Learn and understand how to create Infinite Streams in java 8 with examples in different ways.

1. Overview

In this article, You'll learn how to create infinite streams and how to use them in java 8.

Use Stream.iterate() and Stream.generate() method to create infinite streams in java. These two are declared as static methods and these are called utility methods.

Before diving into the Infinite Stream concepts, Let us understand the few concepts such as Intermediate and Terminal operation from Stream API.

Java 8 and Infinite Streams - How To Create Infinite Streams

2. Stream Intermediate and Terminal Operations

When the stream is created using stream() method internally pipeline will be created. All the methods invoked on Stream are placed inside the pipeline. Whenever methods called at the beginning of the streams are called as  Intermediate Operations.

All the intermediate operations returns Stream<T> instance such as filter(), map() and flatMap() comes under Intermediate Operations. If any method is not returning Stream then it is not Intermediate Operation.

Stream Intermediate Operations:

filter()

map()

flatMap()

distinct()

sorted()

peek()

limit()

skip()

Note: All intermediate operations are immediately executed and only executed after calling the terminal operations. Even though you add 10 intermediate operations to stream, all of these are not executed unless you call at least one terminal operation.

After using the intermediate operations, to collect the final output you need to use anther list of methods such as collect(), count(), anyMatch().

Final output can be List, Set or Map, Optional values and these are produced by only upon invocation of the following list of terminal methods

toArray()

collect()

count()

reduce()

forEach()

forEachOrdered()

min()

max()

anyMatch()

allMatch()

noneMatch()

findAny()

findFirst()

3. Infinite Streams in Java 8

By the time now you should be good with the intermediate and terminal operations and their execution flows.

Why I was saying in the above sections that stream intermediate methods must be followed by at least one terminal operation because when you call to iterate() or generate() methods start putting the data into the stream then it will be added with infinite values. So if intermediate methods are invoked in the pipeline then it will consume the memory and may end up with the OutOfMemoryError. Because of this reason, terminal operations are needed to end the stream processing.

4. Stream.iterate() Infinite Streams

API description for this method.

"Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.

The first element (position 0) in the Stream will be the provided seed. For n > 0, the element at position n, will be the result of applying the function f to the element at position n - 1."

Syntax:

static <T> Stream<T> iterate(T seed, UnaryOperator<T> f)
Let us create an infinite number series that starts from 0 and increment by 1.
Stream.iterate(1, i -> i +1);
Now, this is adding the elements to the stream and there is no limit for that. Before calling the terminal operation such as collect(), it is good practice to use limit() method.
package com.javaprogramto.java8.streams.infinite;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamIterateExample {

    public static void main(String[] args) {

        // Creating a infinite Stream
        Stream<Integer> integerInfiniteStream = Stream.iterate(1, i -> i +1);

        List<Integer> first15Numbers = integerInfiniteStream.limit(15).collect(Collectors.toList());

        System.out.println("integerInfiniteStream with limit 15 : "+first15Numbers);

    }
}

Output:
integerInfiniteStream with limit 15 : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

First created an unlimited numbers generator using Stream.iterate() method and used the limit(15) method to limit the numbers to 15. Finally, Collect the numbers into List as integers.

5. Stream.generate() Infinite Streams


Stream.generate() is used to generate the infinite series of objects using the given Supplier function.

Syntax:
static <T> Stream<T> generate(Supplier<T> s)
This is mainly suitable in scenarios where generate the constants or random series of objects or numbers.

Returns a Stream of infinite objects.

Example to generate random UUID numbers

package com.javaprogramto.java8.streams.infinite;

import java.util.List;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamGenerateExample {

    public static void main(String[] args) {

        // Generates the UUID
        Supplier<UUID> randomUUIDSupplier = () -> UUID.randomUUID();

        List<UUID> uuidList = Stream.generate(randomUUIDSupplier).limit(10).collect(Collectors.toList());

        System.out.println("10 random UUID list : "+uuidList);

    }
}

Output:
10 random UUID list : [2800389f-221f-4258-b892-94c74b71da7c, 6adcfba0-2670-40e9-b7c0-320cd11ef965, 894be0cc-c254-4ea9-b7c6-8e3c19e7c34d, 7d4e65d8-e0e4-4759-b98d-17e472f67c3e, 1c53963c-5b73-4fee-bc14-7e9ed9b2b838, 96dc6394-cb99-4547-bfba-88d79487841b, a0b949e1-a424-4a3d-acd9-8b6d5674bd0a, 4748832c-be4f-4b95-a333-392359a06d46, f82b2a23-9aa9-4c67-a9eb-2fdec8cfe62b, 44c33cd9-b3a7-4a23-be5b-def78893bdf1]

6.Stream.iterate() vs Stream.generate()


The main difference between the Stream iterate() and generate() method is iterate() is to get the objects in a sequential order whereas generate() is to get the objects randomly.

import java.util.List;
import java.util.Random;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamGenerateVSIterate {

    public static void main(String[] args) {

        // Example to generate 10 random numbers from 0 to 20.
        Supplier<Integer> infiniteStream1 = () -> new Random().nextInt(20);

        List<Integer> randomNumbers = Stream.generate(infiniteStream1).limit(10).collect(Collectors.toList());

        System.out.println("10 random numbers list : " + randomNumbers);

        // Example to generate 10 random numbers from 0 to 20.
        Stream<Integer> infiniteStream2 = Stream.iterate(0, i -> i + 1);

        List<Integer> first10Numbers = infiniteStream2.limit(10).collect(Collectors.toList());

        System.out.println("first 10 numbers list : " + first10Numbers);

    }
}

Output:
10 random numbers list : [2, 5, 0, 7, 0, 9, 11, 3, 12, 16]
first 10 numbers list : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


7. Infinite Streams With Custom Objects


As shown in the previous section that generates the random UUID object is an example for custom objects.

Use Stream.generate() to work with an infinite stream of custom objects.

Let us create an example to work with Book custom objects.

Generate infinite Book objects and collect only the first 10 Book objects.

import java.io.Serializable;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CustomObjectsStreamGenerateExample {

    public static void main(String[] args) {

        List<Book> uuidList = Stream.generate(Book::create)
                                                            .limit(10)
                                                            .collect(Collectors.toList());

        System.out.println("10 random UUID list : "+uuidList);

    }
}

class Book implements Serializable {

    private int id;
    private String title;
    private double price;

    public Book(int id, String title, double price) {
        this.id = id;
        this.title = title;
        this.price = price;
    }

    public static Book create(){
        int id = new Random().nextInt(10);
        return new Book(id, "name "+id, id * 2.2);
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", price=" + price +
                '}';
    }
}
Output:
10 random UUID list : [
Book{id=1, title='name 1', price=2.2}, 
Book{id=0, title='name 0', price=0.0},
 Book{id=6, title='name 6', price=13.200000000000001},
 Book{id=3, title='name 3', price=6.6000000000000005},
 Book{id=1, title='name 1', price=2.2},
 Book{id=0, title='name 0', price=0.0},
 Book{id=1, title='name 1', price=2.2},
 Book{id=1, title='name 1', price=2.2},
 Book{id=2, title='name 2', price=4.4},
 Book{id=5, title='name 5', price=11.0}]

8. Conclusion


In this article, you've seen examples of how to create an infinite stream in java 8.

And also how to limit the infinite loop with the limit() method and collect them into a List.

How to work with custom objects?

Differennces between Stream iterate() and generate() methods.

All the examples are over GitHub.



COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java 8 and Infinite Streams - How To Create Infinite Streams
Java 8 and Infinite Streams - How To Create Infinite Streams
Learn and understand how to create Infinite Streams in java 8 with examples in different ways.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjX-35ZAWEj_NpxrqETpkW1m9gW907Lnp4QbhUmgdZcYE3_N1zZDE4V3NeEkP6wLCF1_9nwESx-bcMf9UsM0wSuKBEctoG2qBhzURjMq8egyGeub-HoTDfohvzhmU5TQFZ8fMrnpFj_tQA/w640-h371/Java+8+and+Infinite+Streams+-+How+To+Create+Infinite+Streams.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjX-35ZAWEj_NpxrqETpkW1m9gW907Lnp4QbhUmgdZcYE3_N1zZDE4V3NeEkP6wLCF1_9nwESx-bcMf9UsM0wSuKBEctoG2qBhzURjMq8egyGeub-HoTDfohvzhmU5TQFZ8fMrnpFj_tQA/s72-w640-c-h371/Java+8+and+Infinite+Streams+-+How+To+Create+Infinite+Streams.png
JavaProgramTo.com
https://www.javaprogramto.com/2020/08/java-infinite-streams.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2020/08/java-infinite-streams.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content