1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.concurrent.ConcurrentHashMap;
  9. public class LogDiff {
  10. private static final int RATE_LIMIT = 100; // Lines per second
  11. private static final long TIME_WINDOW = 1000; // milliseconds
  12. public static void main(String[] args) throws IOException {
  13. if (args.length != 2) {
  14. System.out.println("Usage: java LogDiff <file1> <file2>");
  15. return;
  16. }
  17. String file1 = args[0];
  18. String file2 = args[1];
  19. Map<String, Integer> file1Counts = calculateLogCounts(file1);
  20. Map<String, Integer> file2Counts = calculateLogCounts(file2);
  21. List<String> diffs = findDifferences(file1Counts, file2Counts);
  22. for (String diff : diffs) {
  23. System.out.println(diff);
  24. }
  25. }
  26. private static Map<String, Integer> calculateLogCounts(String filePath) throws IOException {
  27. Map<String, Integer> counts = new ConcurrentHashMap<>();
  28. try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
  29. String line;
  30. while ((line = reader.readLine()) != null) {
  31. String trimmedLine = line.trim();
  32. if(!trimmedLine.isEmpty()) {
  33. counts.put(trimmedLine, counts.getOrDefault(trimmedLine, 0) + 1);
  34. }
  35. }
  36. }
  37. return counts;
  38. }
  39. private static List<String> findDifferences(Map<String, Integer> file1Counts, Map<String, Integer> file2Counts) {
  40. List<String> diffs = new ArrayList<>();
  41. // Find lines present in file1 but not in file2
  42. for (Map.Entry<String, Integer> entry : file1Counts.entrySet()) {
  43. if (!file2Counts.containsKey(entry.getKey())) {
  44. diffs.add("Only in " + file1Counts.keySet().iterator().next() + ": " + entry.getKey() + " (" + entry.getValue() + ")");
  45. }
  46. }
  47. // Find lines present in file2 but not in file1
  48. for (Map.Entry<String, Integer> entry : file2Counts.entrySet()) {
  49. if (!file1Counts.containsKey(entry.getKey())) {
  50. diffs.add("Only in " + file2Counts.keySet().iterator().next() + ": " + entry.getKey() + " (" + entry.getValue() + ")");
  51. }
  52. }
  53. // Find lines with different counts
  54. for (Map.Entry<String, Integer> entry : file1Counts.entrySet()) {
  55. if (file2Counts.containsKey(entry.getKey()) && entry.getValue() != file2Counts.get(entry.getKey())) {
  56. diffs.add("Different count for: " + entry.getKey() + " (File1: " + entry.getValue() + ", File2: " + file2Counts.get(entry.getKey()) + ")");
  57. }
  58. }
  59. return diffs;
  60. }
  61. }

Add your comment