Code:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
public class WebTrafficAnalyzer {
public static void main(String[] args) {
String filePath = "C:\\Users\\Sushanth\\Downloads\\[Link]";
int topN = 5; // Number of top sequences to find
try {
List<String> sequences = findTopSequences(filePath, topN);
for (String sequence : sequences) {
[Link](sequence);
}
} catch (IOException e) {
[Link]();
}
}
private static List<String> findTopSequences(String filePath, int topN) throws
IOException {
Map<String, Integer> sequenceCounts = new HashMap<>();
// Read the file and process each line
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
// Skip the header row
[Link]();
while ((line = [Link]()) != null) {
String[] data = [Link](",");
String sessionId = data[1];
String pageId = data[2];
// Skip sessions that have less than 3 pages
if () {
[Link](sessionId, 0);
}
// Increment the count for the session
[Link](sessionId, [Link](sessionId) + 1);
// If the session has visited at least 3 pages, consider it for the
sequence
if ([Link](sessionId) >= 3) {
String sequence = getSessionSequence(sessionId, filePath);
[Link](sequence,
[Link](sequence, 0) + 1);
}
}
}
// Sort the sequences by count in descending order
List<[Link]<String, Integer>> sortedSequences = new
ArrayList<>([Link]());
[Link]([Link]([Link]()));
// Get the top N sequences
List<String> topSequences = new ArrayList<>();
for (int i = 0; i < topN && i < [Link](); i++) {
[Link]<String, Integer> entry = [Link](i);
String sequence = [Link]();
int count = [Link]();
[Link](sequence + ":" + count);
}
return topSequences;
}
private static String getSessionSequence(String sessionId, String filePath)
throws IOException {
List<String> pages = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
// Skip the header row
[Link]();
while ((line = [Link]()) != null) {
String[] data = [Link](",");
String currentSessionId = data[1];
String pageId = data[2];
// Add the page to the sequence if it belongs to the session
if ([Link](sessionId)) {
[Link](pageId);
}
// Stop adding pages once the sequence has 3 pages
if ([Link]() == 3) {
break;
}
}
}
return [Link](",", pages);
}
}
Output: