Generating random mobile numbers with Java 8

Leave a comment
Dev

In a previous post I gave a way of generating random test mobile numbers (Ofcom approved!) using Scala iterators.

Now that Java 8 gives us lambdas and streams I thought I would see what those generators might look like in Java.

Here’s the mobile generator:

Nice, still a one-liner.

Generating pseudo MAC addresses is a little bit more trouble. This supplier uses another nested stream to generate the random hex array.

You use these Supplier functions with the static generate method of Stream. Because the generators create infinite streams we use limit to just get a few values. Eg:

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.