从日志文件中获取mot common ip(优化)

时间:2018-03-17 17:26:48

标签: java intellij-idea optimization lambda stream

我必须从日志文件中获取最常见的ip,  我有一个工作方法但我需要一些帮助来优化它/我必须用流替换som段落。你能帮帮我吗?

  private static String mostCommonIp(String fileName) throws IOException
{
    Map<String, Long> ipAppearanceCount = new HashMap<>();
    String commonMostIp ="";
    List<String> lines = Files.lines(Paths.get(fileName))
            .collect(Collectors.toList());

    List<String> ipAddresses = lines
            .stream()
            .map(line -> line.replaceAll("[ ]+", " ").split(" ")[2])
            .collect(Collectors.toList());

    for (int i = 0; i < ipAddresses.size(); i++) {
        if (!ipAppearanceCount.containsKey(ipAddresses.get(i))){
            ipAppearanceCount.put(ipAddresses.get(i), (long) 1);
        }else{
            ipAppearanceCount.put(ipAddresses.get(i),
ipAppearanceCount.get(ipAddresses.get(i))+1);
        }
    }

    Comparator<? super Entry<String, Long>> maxValueComparator =  
Comparator.comparing(Entry::getValue);

    Optional<Entry<String, Long>> maxValue = ipAppearanceCount.entrySet()
            .stream().max(maxValueComparator);

    return String.valueOf(maxValue);
}

Here is the log file

1 个答案:

答案 0 :(得分:0)

每个步骤后通常不打算收集流。 而是链接下一个操作。

对于大多数操作,已经存在一些有用的收集器。 最后的“单线”就是:

private static String mostCommonIp(String fileName) throws IOException {
    try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
        return stream
            // Get the IP as String. Your code. I hope this works for you.
            .map(line -> line.replaceAll("[ ]+", " ").split(" ")[2])
            // And now we group by the IPs, so we get a Map<String,Long>
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
            // Now we convert that back to a stream
            .entrySet().stream()
            // And find the entry with the highest number.
            .max(Comparator.comparing(Entry::getValue))
            // Get the actual IP out of it
            .map(Entry::getKey)
            // and return the IP with the highest count - or null if not found.
            .orElse(null);
    }
}