I have log entries for a single user that exist in the following format:
[unique id], [start time],[ end time]
So, in the following example entries:
1,1100,1200
2,1030,1130
3,1420,1500
4,1519,1700
Find Sessions ie, group log entries into as 'sessions'. The conditions to determine a session are:
Example: Output should be like:
Session 1: 1, 2
Session 2: 3, 4
Logic what I am thinking is:
I came up with below code but I am confuse on how to work on point 3 logic above.
private static List<String> groupSessions(List<String> inputs) {
List<String> output = new ArrayList<>();
List<LogEntries> entries = new ArrayList<>();
for (String input : inputs) {
String[] arr = input.split(",");
LogEntries entry =
new LogEntries(Integer.parseInt(arr[0]), Integer.parseInt(arr[1]),
Integer.parseInt(arr[2]));
entries.add(entry);
}
// sort it basis on startTime
Collections.sort(entries);
// now iterate the entries list - this is where I am confuse
for (int i = 0; i < entries.size(); i++) {
// do some stuff
}
return output;
}
Some thoughts:
Long story short: you have to first develop the algorithm that tells you how to determine sessions. Then you turn that sequence of instructions into code. The key is to first conceptually dissect the big problem into its smallest parts and to then see how to bring them together.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.