Hallo zusammen. Dieses Mal möchte ich eine funktionale Schnittstelle vorstellen.
public interface Predicate <T>
Methodenbeispiel:
import java.util.function.Predicate;
public class App {
public static void main( String[] args ) {
Predicate <String> pre = (String username) -> {
return "admin".equals(username);
};
System.out.println(pre.test("manager"));
System.out.println(pre.test("admin"));
}
}
Rückgabewert:
false
true
public interface Consumer<T>
import java.util.function.Consumer;
public class App {
public static void main( String[] args ) {
Consumer<String> con = (String message) -> {
System.out.println("Return message: " + message);
System.out.println("The message send succeed");
};
con.accept("Hello world");
con.accept("Lambda");
}
}
Function
public interface Function<T,R>
Beispiel:
import java.util.function.Function;
public class App {
public static void main( String[] args ) {
Function<String, Integer> fun = (String gender) -> {
return ("male".equals(gender))?0:1;
};
System.out.println(fun.apply("male"));
System.out.println(fun.apply("female"));
}
}
Ergebnis:
0
1
Recommended Posts