JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Create Java 8 Stream using Stream.generate() method.


There are various ways to create Streams. In this page, we can see an example on Stream creation using Stream.generate() method.

The syntax is:

static Stream generate(Supplier s)

Supplier is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

package com.java2novice.streams;

import java.util.UUID;
import java.util.stream.Stream;

public class StreamGenerateEx {

	public static void main(String a[]) {

		Stream<UUID> uuidStream = Stream.generate(UUID::randomUUID);
		uuidStream.limit(10).forEach(System.out::println);
	}
}

Output:
2a335867-4812-4351-b40b-feeae68f5ca9
120b362f-c9d5-45bf-a38a-74eb9c2c957f
c83b58b5-7e1d-4c02-b64c-31d8a450441d
448e330a-ca55-4fd4-b4ce-d6fd9cb31df5
ff9cfeed-4539-4001-97b0-5aa31d0a7d95
fe5c4c97-1722-46aa-923b-ee7428c2034c
687dd6fc-5c8a-4d75-bec8-4deba4fa534d
eab1aaf9-7b02-421b-b919-3af6d79d9d3a
6d1e3a14-4551-4151-ba37-1c557741ba25
da0a0025-4717-4b7b-8643-b5cc53d7ce42
<< Previous Program | Next Program >>

Java 8 Streams Examples

  1. How Java 8 Streams work?
  2. Java 8 Streams parallelism introduction.
  3. Explain non-interference behavior of Java 8 Streams.
  4. Create Java 8 Stream using Stream.of() method example.
  5. Create Java 8 Stream using List example.
  6. Create Java 8 Stream using Stream.generate() method.
  7. Java 8 Stream.filter() example.
  8. Java 8 Stream.map() example.
  9. Java 8 Stream flatmap method example.
  10. Java 8 Stream peek method example.
  11. Java 8 Stream distinct method example.
  12. Java 8 Stream sorted method example.
  13. Java 8 Stream limit method example.
  14. Java 8 Stream forEach method example.
  15. Java 8 Stream toArray method example.
  16. Java 8 Stream reduce method example.
  17. Java 8 Stream collect method example.
  18. Java 8 Stream concat method example.
  19. Java 8 Stream anyMatch(), allMatch() and noneMatch() example.
  20. Java 8 Stream findFirst(), findAny() example.
  21. Primitive type Stream example.
Knowledge Centre
Procedural Vs Object-oriented Programs
In procedural program, programming logic follows certain procedures and the instructions are executed one after another. In OOP program, unit of program is object, which is nothing but combination of data and code.

In procedural program, data is exposed to the whole program whereas in OOPs program, it is accessible with in the object and which in turn assures the security of the code.
Famous Quotations
Be yourself; everyone else is already taken.
-- Oscar Wilde

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.