forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
58 lines (41 loc) · 2.63 KB
/
Copy pathMain.java
File metadata and controls
58 lines (41 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package modern.challenge;
import java.util.concurrent.TimeUnit;
public class Main {
private static final String ONLY_DIGITS = "45566336754493420932877387482372374982374823"
+ "749823283974232237238472392309230923849023848234580383485342234287943943094"
+ "234745374657346578465783467843653748654376837463847654382382938793287492326";
private static final String NOT_ONLY_DIGITS = "45566336754493420932877387482372374982374823"
+ "749823283974232237238472392309230923849023848234580383485342234287943943094"
+ "234745374657346578465783467843653748654376837463847654382382938793287492326A";
public static void main(String[] args) {
System.out.println("Input text with only digits: \n" + ONLY_DIGITS + "\n");
System.out.println("Input text with other characters: \n" + NOT_ONLY_DIGITS + "\n");
System.out.println("Character.isDigit() solution:");
long startTimeV1 = System.nanoTime();
boolean onlyDigitsV11 = Strings.containsOnlyDigitsV1(ONLY_DIGITS);
boolean onlyDigitsV12 = Strings.containsOnlyDigitsV1(NOT_ONLY_DIGITS);
displayExecutionTime(System.nanoTime() - startTimeV1);
System.out.println("Contains only digits: " + onlyDigitsV11);
System.out.println("Contains only digits: " + onlyDigitsV12);
System.out.println();
System.out.println("String.matches() solution:");
long startTimeV2 = System.nanoTime();
boolean onlyDigitsV21 = Strings.containsOnlyDigitsV2(ONLY_DIGITS);
boolean onlyDigitsV22 = Strings.containsOnlyDigitsV2(NOT_ONLY_DIGITS);
displayExecutionTime(System.nanoTime() - startTimeV2);
System.out.println("Contains only digits: " + onlyDigitsV21);
System.out.println("Contains only digits: " + onlyDigitsV22);
System.out.println();
System.out.println("Java 8, functional-style solution:");
long startTimeV3 = System.nanoTime();
boolean onlyDigitsV31 = Strings.containsOnlyDigitsV3(ONLY_DIGITS);
boolean onlyDigitsV32 = Strings.containsOnlyDigitsV3(NOT_ONLY_DIGITS);
displayExecutionTime(System.nanoTime() - startTimeV3);
System.out.println("Contains only digits: " + onlyDigitsV31);
System.out.println("Contains only digits: " + onlyDigitsV32);
}
private static void displayExecutionTime(long time) {
System.out.println("Execution time: " + time + " ns" + " ("
+ TimeUnit.MILLISECONDS.convert(time, TimeUnit.NANOSECONDS) + " ms)");
}
}