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
71 lines (51 loc) · 3.15 KB
/
Copy pathMain.java
File metadata and controls
71 lines (51 loc) · 3.15 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
package modern.challenge;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class Main {
private static final String TEXT = "Be strong, be fearless, be beautiful. "
+ "And believe that anything is possible when you have the right "
+ "people there to support you. ";
// Ӝ -> Unicode: \u04DC, Code Point: 1244
// 💕 -> Unicode: \uD83D\uDC95, Code Point: 128149
// 🎼 -> \uD83C\uDFBC, Code Point: 127932
// 😍 ->\uD83D\uDE0D, Code Point: 128525
private static final String TEXT_CP = TEXT + "😍 I love 💕 you Ӝ so much 💕 😍 🎼🎼🎼!";
public static void main(String[] args) {
System.out.println("Input text: \n" + TEXT + "\n");
System.out.println("\n\nASCII or 16 bits Unicode characters (less than 65,535 (0xFFFF)) examples:\n");
System.out.println("HashMap based solution:");
long startTimeV1 = System.nanoTime();
Map<Character, Integer> duplicatesV1 = Strings.countDuplicateCharactersV1(TEXT);
displayExecutionTime(System.nanoTime()-startTimeV1);
System.out.println(Arrays.toString(duplicatesV1.entrySet().toArray()));
// or: duplicatesV1.forEach( (k, v) -> System.out.print(k + "="+ v + ", "));
System.out.println();
System.out.println("Java 8, functional-style solution:");
long startTimeV2 = System.nanoTime();
Map<Character, Long> duplicatesV2 = Strings.countDuplicateCharactersV2(TEXT);
displayExecutionTime(System.nanoTime()-startTimeV2);
System.out.println(Arrays.toString(duplicatesV2.entrySet().toArray()));
// or: duplicatesV2.forEach( (k, v) -> System.out.print(k + "="+ v + ", "));
System.out.println("\n--------------------------------------\n");
System.out.println("Input text: \n" + TEXT_CP + "\n");
System.out.println("\n\nIncluding Unicode surrogate pairs examples:\n");
System.out.println("HashMap based solution:");
long startTimeV3 = System.nanoTime();
Map<String, Integer> duplicatesV3 = Strings.countDuplicateCharactersVCP1(TEXT_CP);
displayExecutionTime(System.nanoTime()-startTimeV3);
System.out.println(Arrays.toString(duplicatesV3.entrySet().toArray()));
// or: duplicatesV3.forEach( (k, v) -> System.out.print(k + "="+ v + ", "));
System.out.println();
System.out.println("Java 8, functional-style solution:");
long startTimeV4 = System.nanoTime();
Map<String, Long> duplicatesV4 = Strings.countDuplicateCharactersVCP2(TEXT_CP);
displayExecutionTime(System.nanoTime()-startTimeV4);
System.out.println(Arrays.toString(duplicatesV4.entrySet().toArray()));
// or: duplicatesV4.forEach( (k, v) -> System.out.print(k + "="+ v + ", "));
}
private static void displayExecutionTime(long time) {
System.out.println("Execution time: " + time + " ns" + " (" +
TimeUnit.MILLISECONDS.convert(time, TimeUnit.NANOSECONDS) + " ms)");
}
}