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);
}
}
-
5What is your question? Does the code not work? What errors does it give you? Does it work, but yields unexpected results? Please specify.Sergey Kalinichenko– Sergey Kalinichenko2018年02月28日 13:27:38 +00:00Commented Feb 28, 2018 at 13:27
-
Sorry I have posted before the code with a static method from another class. I have edited the code it works now like a previous version. I see a message "Files list in folder:C:\Logs".Anton Troitsky– Anton Troitsky2018年02月28日 14:02:21 +00:00Commented Feb 28, 2018 at 14:02
3 Answers 3
Try Function<Path, Path> instead of FuncForPath. This will require a method taking a Path parameter and returning a Path. Note that instance methods always have an "invisible" this parameter, hence Path::getFileName matches that signature.
You'd then call it like this: paths.forEach( p -> funcPath.apply( p ).toString() ); (although you're not doing anything with the returned string, so you probably want to call paths.map( f ).map( Path::toString ).collect( someCollector ); instead.
4 Comments
paths.map(funcPath).map(Path::toString) so it transforms from your path to the path your function returns, then into the string representation. Also note you might want to actually print them by .forEach(System.out::println) or something.How I understand I have resolved this problem. There is code below.
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 -> System.out.println(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);
}
}
1 Comment
FuncForPath interface. Just use Function<Path, Path>.Also, I have deleted the generic specification in interface description.
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 { // there is interface without type <T>
Path func(Path path);
}
class MethodsForFolder {
public static void printFilesInFolder(Path path, FuncForPath 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 -> System.out.println(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);
}
}