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
72 lines (50 loc) · 2.77 KB
/
Main.java
File metadata and controls
72 lines (50 loc) · 2.77 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package modern.challenge;
import java.util.concurrent.TimeUnit;
public class Main {
private static final String TEXT = "oobotooorogshŜoootorgo";
private static final char CHAR = 'Ŝ';
private static final String TEXT_CP = "😍 I love 💕 you Ӝ so much 💕 😍";
private static final String CHAR_CP = "Ӝ"; // Unicode: \u04DC, Code Point: 1244
private static final String CHAR_CPS = "💕"; // Unicode: \uD83D\uDC95, Code Point: 128149
public static void main(String[] args) {
System.out.println("Input text: \n" + TEXT);
System.out.println("Character to remove: " + CHAR + "\n");
System.out.println("StringBuilder based solution:");
long startTimeV1 = System.nanoTime();
String resultV1 = Strings.removeCharacterV1(TEXT, CHAR);
displayExecutionTime(System.nanoTime() - startTimeV1);
System.out.println("Result: \n" + resultV1);
System.out.println();
System.out.println("Regular expression based solution:");
long startTimeV2 = System.nanoTime();
String resultV2 = Strings.removeCharacterV2(TEXT, CHAR);
displayExecutionTime(System.nanoTime() - startTimeV2);
System.out.println("Result: \n" + resultV2);
System.out.println();
System.out.println("Java 8, functional-style solution:");
long startTimeV3 = System.nanoTime();
String resultV3 = Strings.removeCharacterV3(TEXT, CHAR);
displayExecutionTime(System.nanoTime() - startTimeV3);
System.out.println("Result: \n" + resultV3);
System.out.println();
System.out.println("Java 8, function-style solution (code point)");
System.out.println("Input text: \n" + TEXT_CP);
System.out.println("Character to remove: " + CHAR_CP + "\n");
long startTimeV4 = System.nanoTime();
String resultV4 = Strings.removeCharacterV4(TEXT_CP, CHAR_CP);
displayExecutionTime(System.nanoTime() - startTimeV4);
System.out.println("Result: \n" + resultV4);
System.out.println();
System.out.println("Java 8, function-style solution (code point)");
System.out.println("Input text: \n" + TEXT_CP);
System.out.println("Character to remove: " + CHAR_CPS + "\n");
long startTimeV5 = System.nanoTime();
String resultV5 = Strings.removeCharacterV4(TEXT_CP, CHAR_CPS);
displayExecutionTime(System.nanoTime() - startTimeV5);
System.out.println("Result: \n" + resultV5);
}
private static void displayExecutionTime(long time) {
System.out.println("Execution time: " + time + " ns" + " ("
+ TimeUnit.MILLISECONDS.convert(time, TimeUnit.NANOSECONDS) + " ms)");
}
}