java.util.function包定义了几个我正内置功能接口可以在创建时使用lambda表达式或方法的引用。
功能接口仅具有一个单一的方法应用()。它可以接受任何数据类型的对象,并返回任何数据类型的结果。
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 + "\n"; } return names; }; System.out.println(converter.apply(countries)); } }
India Australia England South Africa Srilanka Newzealand West Indies Scotland
一个 供应商接口只有一个单一的方法调用,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(() -> "\nList 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
该用户接口只有一个单一的方法称为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:\n"); Arrays.asList(countries).forEach((country) -> System.out.println(country)); // lambda expression } }
The list of countries: India Australia England South Africa Srilanka Newzealand West Indies
谓词接口 仅具有一个单一的方法 测试()。取决于其变量的值,它可以是对 或错 。
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]