Prior to Java 8, this method would be used to create a list of hidden files:
File[] hiddenFiles = new File("./directory/").listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isHidden();
}
});
In Java 8, this can be shortened to:
File[] hiddenFiles = new File("./directory/").listFiles(File::isHidden);
Returning non-hidden files in the original code was a trivial change: return file.!isHidden(); as a substitute for return file.isHidden();. I cannot recreate this functionality within a single line.
There is no isNotHidden function within the File class. Without creating one (or without deferring to the original, more verbose code), is there a way to recreate it using the new single-line style?
How about this,
File[] hiddenFiles = new File("c:/data").listFiles(f -> !f.isHidden());
Coming in java-11 Predicate.not, until then you can't via a method reference
Predicate.not(File::isHidden)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With