1

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);
    }
}
2
  • 5
    What is your question? Does the code not work? What errors does it give you? Does it work, but yields unexpected results? Please specify. Commented 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". Commented Feb 28, 2018 at 14:02

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

...and you can do 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.
@daniu yes, just added the usage part :)
Soory, It was my bad. I wanted to print the list of files in the console.
Another possibility is .map(f.andThen(Path::toString)).collect(...)
0

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

You can do it that way but there's normally no need for the FuncForPath interface. Just use Function<Path, Path>.
0

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);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.