1

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!

5
  • 6
    "I would like to make my code look more efficient". Simply change the method name to extractFileNumberSuperFast Commented Sep 3, 2018 at 7:46
  • 1
    But seriously, focusing on writing the shortest possible code is a dubious goal. It will lead to writing code that is harder to read. Code is changed infrequently and read frequently. Your #1 priority should be to ensure that it's maintainable, not concise. In this situation, the stream solution is both shorter and just as readable. To use a static method reference, use the class name instead of this: MyClass::extractFileNumber Commented Sep 3, 2018 at 7:51
  • Can you post this please getAllFilesFromDirectory? Commented Sep 3, 2018 at 7:53
  • @Micheal you were right with the answer. Commented Sep 3, 2018 at 7:58
  • @Aris_Kortex I did add the method to the post Commented Sep 3, 2018 at 7:58

1 Answer 1

1

Use stream api can make it shorter, suppose extractFileNumber is in class Main:

return Stream.of(files).map(Main::extractFileNumber).collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

1 Comment

Actually Stream#of effectively calls Arrays#stream underneath, so I'm failing to see how this is an improvement. If not this example is wrong semantic wise. Stream#of should be used for an ad-hoc creation of a stream from an array of elements (it's a static creator afterall). Arrays#stream is much more descriptive when operating with arrays. Finally Stream#of should be used with caution especially with primitive types. For arrays at least, you should prefer Arrays#stream.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.