forked from algorithm010/algorithm010
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRogers.java
More file actions
104 lines (88 loc) · 3.28 KB
/
Rogers.java
File metadata and controls
104 lines (88 loc) · 3.28 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
95
96
97
98
99
100
101
102
package Interview;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class Rogers {
Map<Long, Long> count(Map<String, UserStats>... visits) {
Map<Long, Long> res = new HashMap<>();
if (visits == null ) return res;
for(Map<String, UserStats> m : visits){
if (m!=null){
for(String mkey : m.keySet()){
try{
Long id = Long.parseLong(mkey);
UserStats uStats = m.get(mkey);
if (uStats !=null){
Optional<Long> value = uStats.getVisitCount();
if (value!=null && value.isPresent()){
Long vistCount = value.get();
if (res.containsKey(id)){
vistCount = vistCount + res.get(id);
}
res.put(id,vistCount);
}
}
}catch (NumberFormatException e){
//ignore
}
}
}
}
return res;
}
Map<Long, Long> count2(Map<String, UserStats>... visits) {
Map<Long, Long> res = new HashMap<>();
if (visits == null ) return res;
for(Map<String, UserStats> m : visits) {
if (m != null) {
m.entrySet()
.stream()
.filter(entry -> isValid(entry.getKey(),entry.getValue()))
.forEach((entry) -> {
Long id = Long.valueOf(entry.getKey()) ;
Long vistCount = entry.getValue().getVisitCount().get();
res.put(id,vistCount+ res.getOrDefault(id, new Long(0)));
});
}
}
return res;
}
boolean isValid(String key, UserStats value){
if (key==null || value == null || !isLong(key)){
return false;
}
if (value.getVisitCount() == null || !value.getVisitCount().isPresent())
return false;
return true;
}
boolean isLong(String s){
try{
Long.parseLong(s);
return true;
}catch (NumberFormatException e){
//ignore
}
return false;
}
}
class test {
public static void main(String[] args) {
Map<String, UserStats> visitStats1 = new HashMap<>();
visitStats1.put("123", new UserStats(Optional.of(new Long(2))));
visitStats1.put("456", new UserStats(null));
visitStats1.put(null, null);
visitStats1.put("444", null);
visitStats1.put("567", new UserStats(Optional.empty()));
visitStats1.put("234", new UserStats(Optional.of(new Long(1))));
Map<String, UserStats> visitStats2 = new HashMap<>();
Optional<Long> count2 = Optional.of(new Long(3));
visitStats2.put("123", new UserStats(count2));
Rogers rogers = new Rogers();
Map<Long, Long> res = rogers.count2(visitStats1,visitStats2,null);
// Map<Long, Long> res = rogers.count(null);
res.forEach((K,V)->{
System.out.println("Key:"+K+"; Value:"+V);
});
}
}