Contents
  1. 1. Random Values
  2. 2. Regex Expression

Random Values

Random Integer

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

1
2
3
4
5
6
7
8
9
10
11
12
// 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();
1
2
3
4
// Java 1.7 or later
int min = 0, max = 10;
ThreadLocalRandom random = ThreadLocalRandom.current();
int randomNum = random.nextInt(min, max + 1);
1
2
3
4
// 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.

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

Random float

// TODO

Random String

// TODO

Regex Expression

Get Match String

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.groupCount()); // 1, 1
System.out.println(matcher.group()); // a Java developer, a Web developer
System.out.println(matcher.group(1)); // 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 ?

Match special characters

1
[\\special_character]

For example:

1
[\\(\\)\\[\\]]
Contents
  1. 1. Random Values
  2. 2. Regex Expression