I have one problem in understanding of Java 8 associated with reference to methods as a parameter to static methods. There is my code where I can't find how to send a reference to a method which doesn't have any parameters and must be a method for an object of the definite class.
So, I want that the method which I send to static function may be used for the object in the static method.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
interface FuncForPath<T> {
T func(T path);
}
class MethodsForFolder {
public static void printFilesInFolder(Path path, FuncForPath<Path> funcPath) {
Stream<Path> paths = null;
try {
paths = Files.list(path);
} catch (IOException e) {
System.out.println(
"The problems have been appeared during reading folder: " + path.toAbsolutePath().toString());
e.printStackTrace();
}
System.out.println("Files list in folder:" + path.toAbsolutePath().toString());
paths.forEach(p -> funcPath.func(p).toString()); // I don't know to how to write this code to perform
}
}
public class TestRefToIntance {
public static String testWindowsFloder = "C://Logs";
public static void main(String[] args) {
Path path = Paths.get(testWindowsFloder);
// I want that this 2 methods are performed depending on transfered methods
// reference
MethodsForFolder.printFilesInFolder(path, Path::toAbsolutePath);
MethodsForFolder.printFilesInFolder(path, Path::getFileName);
}
}