Java 中有哪些内置函数式接口?
**java.util.function** 包定义了几个内置函数式接口,在创建 **lambda 表达式** 或 **方法引用** 时可以使用这些接口。
内置函数式接口
1) Function 接口
**Function 接口** 只有一个单独的方法 **apply()**。它可以接受任何数据类型的对象,并返回任何数据类型的结果。
示例
import java.util.*; import java.util.function.*; public class FunctionTest { public static void main(String args[]) { String[] countries = {"India", "Australia", "England", "South Africa", "Srilanka", "Newzealand", "West Indies", "Scotland"}; Function<String[], String> converter = (all) -> { // lambda expression String names = ""; for(String n : all) { String result = n.substring(0, n.indexOf("")); result = n.substring(n.indexOf("")) + " " + result; names += result + "
"; } return names; }; System.out.println(converter.apply(countries)); } }
输出
India Australia England South Africa Srilanka Newzealand West Indies Scotland
2) Supplier 接口
**Supplier 接口** 只有一个单独的方法,称为 **get()**。它不接受任何参数,并返回任何数据类型的一个对象。
示例
import java.util.*; import java.util.function.*; public class SupplierTest { private static void printNames(Supplier<String> arg) { System.out.println(arg.get()); } private static void listBeginWith(List<String> list, Predicate<String> valid) { printNames(() -> "
List of countries:"); list.forEach(country -> { // lambda expression if(valid.test(country)) { printNames(() -> country); } }); } public static void main(String[] args) { String[] countries = {"India", "Australia", "England", "South Africa", "Srilanka", "Newzealand", "West Indies"}; List<String> countryList = Arrays.asList(countries); listBeginWith(countryList, (s) -> s.startsWith("I")); listBeginWith(countryList, (s) -> s.contains("I")); listBeginWith(countryList, (s) -> s.endsWith("ia")); } }
输出
List of countries: India List of countries: India West Indies List of countries: India Australia
3) Consumer 接口
**Consumer 接口** 只有一个单独的方法,称为 **accept()**。它接受任何数据类型的单个参数,并且不返回任何结果。
示例
import java.util.*; import java.util.function.*; public class ConsumerTest { public static void main(String[] args) { String[] countries = {"India", "Australia", "England", "South Africa", "Srilanka", "Newzealand", "West Indies"}; System.out.print("The list of countries:
"); Arrays.asList(countries).forEach((country) -> System.out.println(country)); // lambda expression } }
输出
The list of countries: India Australia England South Africa Srilanka Newzealand West Indies
4) Predicate 接口
**Predicate 接口** 只有一个单独的方法**test()**。它可以是 **true** 或 **false**,具体取决于其变量的值。
示例
import java.util.*; import java.util.function.*; public class PredicateTest { private static List getBeginWith(List<String> list, Predicate<String> valid) { List<String> selected = new ArrayList<>(); list.forEach(country -> { // lambda expression if(valid.test(country)) { selected.add(country); } }); return selected; } public static void main(String[] args) { String[] countries = {"India", "Australia", "England", "South Africa", "Srilanka", "Newzealand", "West Indies"}; List<String> countryList = Arrays.asList(countries); System.out.println(getBeginWith(countryList, (s) -> s.startsWith("A"))); System.out.println(getBeginWith(countryList, (s) -> s.contains("W"))); System.out.println(getBeginWith(countryList, (s) -> s.endsWith("nd"))); } }
输出
[Australia] [West Indies] [England, Newzealand]
广告