Anton Troitsky
Anton Troitsky

Reputation: 87

Java. Reference to method without parameters

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

Upvotes: 1

Views: 651

Answers (3)

Anton Troitsky
Anton Troitsky

Reputation: 87

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

Upvotes: 0

Anton Troitsky
Anton Troitsky

Reputation: 87

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

Upvotes: 0

Thomas
Thomas

Reputation: 88747

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.

Upvotes: 1

Related Questions