例:
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();
}
ここでは 2 つ目のメソッドが 1 つ目のメソッドとほぼ同じであるため、1 つ目のメソッドを 2 つ目のメソッドの実装で再利用できます。
クイックフィックスを適用すると、結果は次のようになります。
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 の新機能です