Created
February 3, 2017 15:30
-
-
Save m-x-k/251be9c105a09b0d00188c8471dbc53b to your computer and use it in GitHub Desktop.
java random string list generator example with streams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.Random; | |
import java.util.function.Supplier; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
class TestRandomStringGenerator { | |
private static Supplier<String> randomDischargeId = () -> RandomStringUtils.randomConsonant(8); | |
public static void main(String[] args) { | |
List<String> collect = Stream.generate(randomDischargeId).limit(100).collect(Collectors.toList()); | |
System.out.println(collect); | |
} | |
} | |
class RandomStringUtils { | |
private static final String CONSONANT = "BCDFGHJKLMNPQRSTVWXYZ"; | |
private static Random random = new Random(); | |
public static String randomConsonant(int len) { | |
StringBuilder sb = new StringBuilder(len); | |
for (int i = 0; i < len; i++) { | |
sb.append(CONSONANT.charAt(random.nextInt(CONSONANT.length()))); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment