-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRidersApp.java
More file actions
80 lines (73 loc) · 2.61 KB
/
RidersApp.java
File metadata and controls
80 lines (73 loc) · 2.61 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
package rider;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class RidersApp {
public static void main(String[] args) throws IOException {
String path = "/Users/rgupta30/old_laptop_bkup/bk4/tutorials/personal_git_hub/java-samples/src/rider/riders.txt";
RidersApp app = new RidersApp();
Map<String, TreeMap<Integer, String>> ridersMap = app.readFileAndMapByRider(path);
System.out.println(ridersMap);
Map<String, Integer> seqMap = app.mapSequence(ridersMap);
System.out.println(seqMap);
}
protected Map<String, TreeMap<Integer, String>> readFileAndMapByRider(String filePath) throws IOException {
Map<String, TreeMap<Integer, String>> map = new HashMap();
try (Stream<String> IStream = Files.lines(Paths.get(filePath)).skip(1)) {
IStream.forEach(line -> {
String[] tokens = line.split(" ");
System.out.println("tokens:" + tokens.length + ":line:" + line);
if (!map.containsKey(tokens[0])) {
map.put(tokens[0], new TreeMap<Integer, String>());
}
TreeMap<Integer, String> timeMap = map.get(tokens[0]);
timeMap.put(Integer.valueOf(tokens[2]), tokens[1]);
map.put(tokens[0], timeMap);
});
}
return map;
}
protected Map<String, Integer> mapSequence(Map<String, TreeMap<Integer, String>> map) {
Map<String, Integer> seqMap = new HashMap<String, Integer>();
for (String user: map.keySet()) {
TreeMap<Integer, String> timeMapForUser = map.get(user);
if (timeMapForUser.size() < 3) {
continue;
} //else {
//List<Integer> times = new ArrayList<Integer>();
/*for(int key: timeMap.keySet()) {
times.add(key);
}*/
List<Integer> timesForUser = timeMapForUser.keySet().stream().collect(Collectors.toList());
List<String> allSeqsForUser = formAllSeq(timesForUser, timeMapForUser);
for(String seq: allSeqsForUser) {
if (!seqMap.containsKey(seq)) {
seqMap.put(seq, 1);
} else {
int count = seqMap.get(seq);
seqMap.put(seq, count + 1);
}
}
//}
}
return seqMap;
}
protected List<String> formAllSeq(List<Integer> time, TreeMap<Integer, String> timeMap) {
List<String> result = new ArrayList<String>();
for(int i = 0 ; i < time.size() - 2; i++) {
for (int j = i+1; j < time.size() - 1; j++) {
for (int k = j+1; k < time.size(); k++) {
result.add(timeMap.get(time.get(i)) + "," + timeMap.get(time.get(j)) + "," + timeMap.get(time.get(k)));
}
}
}
return result;
}
}