new Random().nextInt(min, max+1) return [min, max]
1 2 3 4 5 6 7 8 9 10 11 12
// Java 8 intsize=1, min = 0, max = 10; ThreadLocalRandomrandom= ThreadLocalRandom.current(); intrandomNumber= random.ints(size, min, max + 1).findFirst().getAsInt();
intsize=5, min = 0, max = 10; ThreadLocalRandomrandom= ThreadLocalRandom.current(); int[] randomNumbers = random.ints(size, min, max + 1).toArray();
intsize=5, min = 0, max = 10; Randomrandom=newRandom(); int[] randomNumbers = random.ints(size, min, max + 1).toArray();
1 2 3 4
// Java 1.7 or later intmin=0, max = 10; ThreadLocalRandomrandom= ThreadLocalRandom.current(); intrandomNum= random.nextInt(min, max + 1);
1 2 3 4
// Before Java 1.7 intmin=0, max = 10; Randomrandom=newRandom(); intrandomNum= 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.
matches() - Checks if the regexp matches the complete string.
1 2 3 4 5 6 7 8
Stringsource="hello there, I am a Java developer. She is a Web developer."; Patternpattern= Pattern.compile("a (.*?) developer"); Matchermatcher= pattern.matcher(source); System.out.println(matcher.matches()); // false
Stringsource="hello there, I am a Java developer. She is a Web developer."; Patternpattern= Pattern.compile("a (.*?) developer", Pattern.CASE_INSENSITIVE); Matchermatcher= pattern.matcher(source); // find all match strings while (matcher.find()) { System.out.println(matcher.group()); // a Java developer, a Web developer for (inti=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
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Stringsource="hello there, I am a Java developer. She is a Web developer."; Stringregex="a (.*?) developer"; intgroupToReplace=1; Stringreplacement="Good"; Matchermatcher= Pattern.compile(regex).matcher(source); StringBuilderresult=newStringBuilder(source); intadjust=0; while (matcher.find()) { intstart= matcher.start(groupToReplace); intend= 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.