Java – How the method reference syntax works in Java 8

How the method reference syntax works in Java 8… here is a solution to the problem.

How the method reference syntax works in Java 8

I’m a little confused about how method references work in Java 8. I wrote the following code snippet to filter hidden files in a folder. They are producing the right results. I don’t understand how the method signature of the -> listFiles method works for option 2 of this snippet.

This is what I found in the Java 8 documentation

  1. files[] listFiles().
  2. File[] listFiles(FileFilter filter).
  3. File[] listFiles(FilenameFilter filter).
File[] hidden = f.listFiles((p)-> p.isHidden()); Option 1 - function signature matching (based on my understanding)
for (int i = 0; i < hidden.length; i++) {
    System.out.println(hidden[i]);
}
System.out.println("================================");
File[] hidden1 = f.listFiles(File::isHidden); Option 2 - how this method call is working
for (int i = 0; i < hidden1.length; i++) {
    System.out.println(hidden1[i]);
}

Related Problems and Solutions