Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return Not-Hidden Files [duplicate]

Tags:

java

java-8

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?

like image 683
MK_Codes Avatar asked Dec 02 '25 09:12

MK_Codes


2 Answers

How about this,

File[] hiddenFiles = new File("c:/data").listFiles(f -> !f.isHidden());
like image 181
Ravindra Ranwala Avatar answered Dec 04 '25 23:12

Ravindra Ranwala


Coming in java-11 Predicate.not, until then you can't via a method reference

Predicate.not(File::isHidden)
like image 33
Eugene Avatar answered Dec 05 '25 00:12

Eugene