-
-
Save ramshers/faf5e3d2bb3d3d134cf21a20ed8cc9e5 to your computer and use it in GitHub Desktop.
Assigning method references to Functional Interfaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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