I would like to make my code look more efficient. Currently I got something like this:
private static String[] getAllFilesFromDirectory(String path) {
File file = new File(path);
String fileNamePattern = file.getName() + ".*\\.[0-9]*\\.GOOD";
File dir = new File(file.getParent());
String[] files = dir.list((currDir, currName) -> currName.matches(fileNamePattern));
return files;
}
private static List<Integer> getAllFileNumbers(String path) {
String[] files = getAllFilesFromDirectory(path);
List<Integer> fileNumbers = new ArrayList<>();
for (String currentFile : files) {
fileNumbers.add(extractFileNumber(currentFile));
}
return fileNumbers;
}
private static Integer extractFileNumber(String fileName) {
return Integer.parseInt(fileName.split("\\.")[2]);
}
First thing first. To getAllFileNumbers method I am passing path to directory, after that I am getting array of files in this directory with specific pattern. And from every file I need to extract number.
Example:
test.txt.1.fail
test2.txt.2.good
test3.pdf.1.good
Here I am returning list of following numbers : 1, 2, 1.
What I would like to change? Probably use stream api instead of foreach loop. First idea was something like this:
Arrays.stream(files).map(this::extractFileNumber).collect(Collectors.toList());
But because the methods are static i cant use it like this.
Obviously I could move extractFileNumber inside of lambda but I would like to keep my method separately.
Maybe someone has other ideas, please feel free to post any ideas. Cheers!
extractFileNumberSuperFastthis:MyClass::extractFileNumbergetAllFilesFromDirectory?