Last active
February 22, 2021 09:43
-
-
Save tkaczenko/8d3adb1285fdd0655e2c6ab66dea7ed6 to your computer and use it in GitHub Desktop.
Generate random String in Java using Stream API
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.*; | |
public final class RandomString { | |
public static String generateRandomString(Random random, int length) { | |
return random.insts(48, 122) | |
.filter(i -> (i < 57 || i > 65) && (i < 90 || i > 97)) | |
.mapToObj(i -> (char) i) | |
.limit(length) | |
.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append) | |
.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment