-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTryJava8.java
More file actions
94 lines (74 loc) · 2.44 KB
/
TryJava8.java
File metadata and controls
94 lines (74 loc) · 2.44 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.java8.learn.one;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.function.BiFunction;
import java.util.function.IntFunction;
import java.util.function.ToIntFunction;
import java.util.stream.Collectors;
public class TryJava8 {
public static void main(String[] args) {
List<String> a = new ArrayList<String>();
a.add("8");
a.add("899");
a.add("7");
a.add("3");
a.add("89");
a.add("29");
a.add("71");
a.add("9");
IntFunction<String> intf = ss -> {
System.out.println(ss);
return String.valueOf(ss);
};
List<String> s = new Random().ints(1, 10000).limit(100000).mapToObj(intf).collect(Collectors.toList());
//List<String> num = Stream.iterate(1, i -> i + 1).limit(100000).collect(Collectors.toList());
Map<Object,Long> myMap = s.stream().collect(Collectors.groupingBy(z->z,Collectors.counting()));
Map<Object, Long> finalMap = new LinkedHashMap<>();
myMap.entrySet().stream()
.sorted(Map.Entry.comparingByValue().reversed())
.forEachOrdered(e->finalMap.put(e.getKey(), e.getValue()))
;
ToIntFunction<String> toInt = (i) -> {
return new Integer(i);
};
BiFunction<String, String, Integer> comp = (q, w) -> {
if (toInt.applyAsInt(q + w) > toInt.applyAsInt(w + q)) {
return -1;
}
return 1;
};
Comparator<String> big = (f, qq) -> {
return comp.apply(f, qq);
};
MyComparer m = new MyComparer();
for (int i = 0; i < a.size(); i++) {
// System.out.println(m.compare(a.get(i), a.get(i)));
}
long startTime = System.nanoTime();
System.out.println(s.size());
String out = s.stream().sorted(big).collect(Collectors.joining(" | "));
// System.out.println(out);
System.out.println("Time taken : " + (System.nanoTime() - startTime));
System.out.println(s.size());
String out1 = s.parallelStream().sorted(big).collect(Collectors.joining(" | "));
// System.out.println(out1);
System.out.println("Time taken : " + (System.nanoTime() - startTime));
}
}
class MyComparer implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
System.out.println("Comparing a :" + o1 + " and b : " + o2);
if (o1.length() > o2.length()) {
return 1;
} else if (o1.length() < o2.length()) {
return -1;
} else {
return 0;
}
}
}