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.

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
...the last 150 posts
...the last 10 comments
License