기존 static 메서드와 동일한 Java 코드 조각을 보고하고 기존의 static 메서드를 재사용하도록 제안합니다. 기존 메서드를 재사용하면 코드가 짧아지고 가독성이 좋아집니다.

예:


  static List<String> readFileAndTrim(Path path) throws IOException {
    List<String> lines = Files.readAllLines(path);
    return lines.stream().map(String::trim).toList();
  }
  
  static List<String> readFileAndTrim(String path) throws IOException {
    Path p = Path.of(path);
    List<String> lines = Files.readAllLines(p);
    return lines.stream().map(String::trim).toList();
  }
여기서 두 번째 메서드는 첫 번째 메서드와 상당히 유사하므로, 첫 번째 메서드를 구현에 재사용할 수 있습니다. 빠른 수정이 적용된 후 결과는 다음과 같습니다.

  static List<String> readFileAndTrim(Path path) throws IOException {
    List<String> lines = Files.readAllLines(path);
    return lines.stream().map(String::trim).toList();
  }

  static List<String> readFileAndTrim(String path) throws IOException {
    Path p = Path.of(path);
    return readFileAndTrim(p);
  }

2024.1의 새로운 기능