Per the java.util.function Javadoc,
Since: 1.8
So upgrade to Java 8, or try to find an older version of the tutorial.
I'm new at this. How can you tell what you are running? I'm using Eclipse
To determine your current Java version in eclipse, go to
Help -> About Eclipse -> Installation Details (Button in
lower Left) -> Configuration pane
Look for the line java.specification.version - on my machine that is
java.specification.version=1.8
Or the line java.runtime.version - on my machine that is
java.runtime.version=1.8.0_11-b12
Answer from Elliott Frisch on Stack OverflowPer the java.util.function Javadoc,
Since: 1.8
So upgrade to Java 8, or try to find an older version of the tutorial.
I'm new at this. How can you tell what you are running? I'm using Eclipse
To determine your current Java version in eclipse, go to
Help -> About Eclipse -> Installation Details (Button in
lower Left) -> Configuration pane
Look for the line java.specification.version - on my machine that is
java.specification.version=1.8
Or the line java.runtime.version - on my machine that is
java.runtime.version=1.8.0_11-b12
i solved this problem by trying following solution
Project > Properties > Java Build Path
Select Libraries tab
Select JRE System Library
Click Edit button
Choose an alternate JRE (jre1.8.0_20)
Click Finish button
You will need to pass myfunc as a method reference.
Try this:
public void callFunc() {
fSupplier(this::myfunc);
}
Your method have one parameter of type Supplier<Integer> so you have three choices to pass as argument to this method (the idea is to pass an instance of a class who implements the functional interface Supplier):
A lambda expression
public void callFunc() { fSupplier(()->Integer.valueOf(1)); }A method reference
public void callFunc() { fSupplier(this::myfunc); }An anonymous inner class
public void callFunc(){ fSupplier(new Supplier<Integer>() { @Override public Integer get() { return Integer.valueOf(1); } }); }
Assuming that your logger is a java.util.logging.Logger . . .
According to the Javadoc for Logger.info, it expects a Supplier<String>, and you're giving it a Supplier<Double>.
To fix this, you need to give it a Supplier<String>. You can write one either like this:
Copyfinal Supplier<String> randomSupplier =
new Supplier<String>() {
public String get() {
return Double.toString(Math.random());
}
};
or like this:
Copyfinal Supplier<String> randomSupplier =
() -> Double.toString(Math.random());
You can even write:
Copylogger.info(() -> Double.toString(Math.random()));
and Java will magically infer that your lambda is meant to be a Supplier<String> (because the other overload of info doesn't take a functional interface type).
You can try this way getting Supplier in java 8 way and logging by converting Supplier to String
CopySupplier<Double> randomSupplier = () -> Math.random();
info(randomSupplier);
private void info(Supplier<Double> randomSupplier) {
System.out.println(randomSupplier.get());
}