import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class LogDiff {
private static final int RATE_LIMIT = 100; // Lines per second
private static final long TIME_WINDOW = 1000; // milliseconds
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.out.println("Usage: java LogDiff <file1> <file2>");
return;
}
String file1 = args[0];
String file2 = args[1];
Map<String, Integer> file1Counts = calculateLogCounts(file1);
Map<String, Integer> file2Counts = calculateLogCounts(file2);
List<String> diffs = findDifferences(file1Counts, file2Counts);
for (String diff : diffs) {
System.out.println(diff);
}
}
private static Map<String, Integer> calculateLogCounts(String filePath) throws IOException {
Map<String, Integer> counts = new ConcurrentHashMap<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
String trimmedLine = line.trim();
if(!trimmedLine.isEmpty()) {
counts.put(trimmedLine, counts.getOrDefault(trimmedLine, 0) + 1);
}
}
}
return counts;
}
private static List<String> findDifferences(Map<String, Integer> file1Counts, Map<String, Integer> file2Counts) {
List<String> diffs = new ArrayList<>();
// Find lines present in file1 but not in file2
for (Map.Entry<String, Integer> entry : file1Counts.entrySet()) {
if (!file2Counts.containsKey(entry.getKey())) {
diffs.add("Only in " + file1Counts.keySet().iterator().next() + ": " + entry.getKey() + " (" + entry.getValue() + ")");
}
}
// Find lines present in file2 but not in file1
for (Map.Entry<String, Integer> entry : file2Counts.entrySet()) {
if (!file1Counts.containsKey(entry.getKey())) {
diffs.add("Only in " + file2Counts.keySet().iterator().next() + ": " + entry.getKey() + " (" + entry.getValue() + ")");
}
}
// Find lines with different counts
for (Map.Entry<String, Integer> entry : file1Counts.entrySet()) {
if (file2Counts.containsKey(entry.getKey()) && entry.getValue() != file2Counts.get(entry.getKey())) {
diffs.add("Different count for: " + entry.getKey() + " (File1: " + entry.getValue() + ", File2: " + file2Counts.get(entry.getKey()) + ")");
}
}
return diffs;
}
}
Add your comment