Skip to content

Instantly share code, notes, and snippets.

@ramshers
Created April 2, 2023 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramshers/faf5e3d2bb3d3d134cf21a20ed8cc9e5 to your computer and use it in GitHub Desktop.
Save ramshers/faf5e3d2bb3d3d134cf21a20ed8cc9e5 to your computer and use it in GitHub Desktop.
Assigning method references to Functional Interfaces
package com.example.boxes;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
@Component
public class RefMs implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Hello from RefMs");
List<String> sList = List.of("a", "b", "c", "d", "e", "", "f", " ", " ", "j");
String myString = "mString";
Supplier f = myString::isBlank;
Function f2 = myString::equals;
Predicate p = String::isBlank; // compilation error: Non-static method cannot be referenced from a static context
Predicate<String> pp = String::isBlank; //this thing works
sList.stream()
.filter(Predicate.not(String::isBlank))
.forEach(System.out::println);
}
public void newMethod(){
Predicate p = String::isBlank; // compilation error: Non-static method cannot be referenced from a static context
Predicate<String> pp = String::isBlank; //this thing works
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment