Java 8: Infinite Stream of UUIDs


import java.util.UUID;
import java.util.stream.Stream;
import org.junit.Test;

@Test
public void infiniteUUIDStream() {
   Stream<String> uuidStream = Stream.
		   generate(UUID::randomUUID).
		   map(u -> u.toString());

   uuidStream.
		   limit(10).
		   forEach(System.out::println);
}


Now you only need a few asserts to verify that the infinite Stream only comprises unique values :-)

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 8, Java 9: Bootstrap, Effective, Architectures, Single Page Apps, Progressive Web Apps, HTML 5, ES 6, CSS 3 and Microservices

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

Comments:

It's always a pleasure to read your blog . Has inspired me to start today the new NetBeans IDE and to experiment with your example .

Posted by Bernd on November 25, 2015 at 09:59 AM CET #

Nice.

We can also replace u -> u.toString() with UUID::toString

Philip

Posted by Philip Schwarz on November 26, 2015 at 09:31 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
realworldpatterns.com
Online Workshops
...the last 150 posts
...the last 10 comments
License