Java Code Examples: Utility classes

Random Values

Random Integer

new Random().nextInt(min, max+1) return [min, max]

// Java 8
int size = 1, min = 0, max = 10;
ThreadLocalRandom random = ThreadLocalRandom.current();
int randomNumber = random.ints(size, min, max + 1).findFirst().getAsInt();

int size = 5, min = 0, max = 10;
ThreadLocalRandom random = ThreadLocalRandom.current();
int[] randomNumbers = random.ints(size, min, max + 1).toArray();

int size = 5, min = 0, max = 10;
Random random = new Random();
int[] randomNumbers = random.ints(size, min, max + 1).toArray();
// Java 1.7 or later
int min = 0, max = 10;
ThreadLocalRandom random = ThreadLocalRandom.current();
int randomNum = random.nextInt(min, max + 1);
// Before Java 1.7
int min = 0, max = 10;
Random random = new Random();
int randomNum = random.nextInt((max - min) + 1) + min;

Math.random() return [0, 1)

Random.nextInt(n) is more efficient than Math.random() * n

Math.random() uses Random.nextDouble() internally. Random.nextDouble() uses Random.next() twice to generate a double that has approximately uniformly distributed bits in its mantissa, so it is uniformly distributed in the range 0 to 1-(2^-53).

Random.nextInt(n) uses Random.next() less than twice on average- it uses it once, and if the value obtained is above the highest multiple of n below MAX_INT it tries again, otherwise is returns the value modulo n (this prevents the values above the highest multiple of n below MAX_INT skewing the distribution), so returning a value which is uniformly distributed in the range 0 to n-1.

int randomNum = min + (int) (Math.random() * ((max - min) + 1));

Random float

// TODO

Random String

// TODO

Regex Expression

matches() - Checks if the regexp matches the complete string.

String source = "hello there, I am a Java developer. She is a Web developer.";
Pattern pattern = Pattern.compile("a (.*?) developer");
Matcher matcher = pattern.matcher(source);
System.out.println(matcher.matches()); // false

Pattern pattern2 = Pattern.compile(".*a (.*?) developer.*");
Matcher matcher2 = pattern2.matcher(source);
System.out.println(matcher2.matches()); // true

find() - Get matching substrings

String source = "hello there, I am a Java developer. She is a Web developer.";
Pattern pattern = Pattern.compile("a (.*?) developer", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(source);
// find all match strings
while (matcher.find()) {
System.out.println(matcher.group()); // a Java developer, a Web developer
for (int i = 1; i < matcher.groupCount(); i++) {
System.out.println(matcher.group(i)); // Java, Web
}
}
// reset matcher for reuse
matcher.reset();
// only find once
if (matcher.find()) {
System.out.println(matcher.group()); // a Java developer
System.out.println(matcher.group(1)); // Java
}

Replace group string

String source = "hello there, I am a Java developer. She is a Web developer.";
String regex = "a (.*?) developer";
int groupToReplace = 1;
String replacement = "Good";
Matcher matcher = Pattern.compile(regex).matcher(source);
StringBuilder result = new StringBuilder(source);
int adjust = 0;
while (matcher.find()) {
int start = matcher.start(groupToReplace);
int end = matcher.end(groupToReplace);
result.replace(start + adjust, end + adjust, replacement);
adjust += replacement.length() - (end - start);
}
System.out.println(result); //hello there, I am a Good developer. She is a Good developer.

Non-greedy regular expression

Followed by the ?. For example .*?

Match special characters

[\\special_character]

For example:

[\\(\\)\\[\\]]

Match multiple line string

// Pattern.DOTALL
Pattern.compile("regexStr", Pattern.DOTALL);

Match case-insensitive string

// Pattern.CASE_INSENSITIVE
Pattern.compile("regexStr", Pattern.CASE_INSENSITIVE)
// (?i) enables case-insensitivity
String regexStr = "(?i).*";
src.matches(regexStr);
// convert string cases
src.toLowerCase();
src.matches(regexStr);

Multiple flags

Pattern.compile("regexStr", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);