先引入一篇介绍的文章:JDK8 函数式接口 -干货使用Function 详细讲解快速上手
案例转载自大佬文章
JDK8的function接口,里面包含四个方法apply
、andThen
、compose
、identity
public class FunctionOneExample{
public static void main(String args[]){
Function<User, String> funcUserToString= (User e)-> {return e.getName();};
List<User> UserList=
Arrays.asList(new User("A-0", 13),
new User("A-1", 25),
new User("B-2", 10),
new User("C-3", 15),
new User("D-4", 29));
List<String> userNameList=convertUserListToNamesList(UserList, funcUserToString);
userNameList.forEach(System.out::println);
}
public static List<String> convertUserListToNamesList(List<User> UserList, Function<User, String> funcUserToString){
List<String> userNameList=new ArrayList<String>();
for(User user:UserList){
userNameList.add(funcUserToString.apply(user));
}
return userNameList;
}
}
输出结果:
A-0
A-1
B-2
C-3
D-4
Function
函数“funcUserToString”的内容是返回User的name。
funcUserToString.apply(user)的意思调用这个函数,得到函数返回的映射值。
public class FunctionAndThenOneExample{
public static void main(String args[]){
Function<User, String> funcUserToString= (User e)-> {return e.getName();};
List<User> UserList=
Arrays.asList(new User("Tike-one", 13),
new User("Hello-world", 25),
new User("Earth-one", 10),
new User("Nanshou-one", 15),
new User("Dogkeep-one", 29));
Function<String,String> initialFunction= (String s)->s.substring(0,1);
List<String> userNameListInitials=convertUserListToNamesList(UserList, funcUserToString.andThen(initialFunction));
userNameListInitials.forEach(str->{System.out.print(" "+str);});
}
public static List<String> convertUserListToNamesList(List<User> UserList, Function<User, String> funcUserToString){
List<String> userNameList=new ArrayList<String>();
for(User user:UserList){
userNameList.add(funcUserToString.apply(user));
}
return userNameList;
}
}
输出结果:
T H E N D
UserList, funcUserToString.andThen(initialFunction):在函数funcUserToString.apply(user)的时候,先执行main方法里的funcUserToString,获取到了用户名,然后调用initialFunction函数截取第一个字符。
文档定义如下:
也就是说,after函数会在函数调用后再调用。
public class FunctionComposeExample{
public static void main(String args[]){
Function<User, String> funcUserToString= (User e)-> {return e.getName();};
Function<User, User> funcuserFirstName=
(User e)-> {int index= e.getName().indexOf("-");
String firstName=e.getName().substring(0,index);
e.setName(firstName);
return e;};
List<User> UserList=
Arrays.asList(new User("Tike one", 13),
new User("Hello-world", 25),
new User("Earth-one", 10),
new User("Nanshou-one", 15),
new User("Dogkeep-one", 29));
List<String> userFirstNameList= convertUserListToNamesList(UserList,funcUserToString.compose(funcuserFirstName));
userFirstNameList.forEach(str->{System.out.print(" "+str);});
}
public static List<String> convertUserListToNamesList(List<User> UserList, Function<User, String> funcUserToString){
List<String> userNameList=new ArrayList<String>();
for(User user:UserList){
userNameList.add(funcUserToString.apply(user));
}
return userNameList;
}
}
结果
Tike Hello Earth Nanshou Dogkeep
funcUserToString.compose(funcuserFirstName):和andThen方法相反,compose方法会先执行参数里的函数funcuserFirstName,然后将得到的结果返回给funcUserToString。
文档定义如下:
先执行函数before,得到的结果返回给调用函数。
public class FunctionTRIdentityExample{
public static void main(String args[]){
List UserList=
Arrays.asList(new User("Tike-one", 13),
new User("Hello-world", 25),
new User("Earth-one", 10),
new User("Nanshou-one", 15),
new User("Dogkeep-one", 29));
List userNameListInitials=applyIdentityTouserList(UserList, Function.identity());
userNameListInitials.forEach(System.out::println);
}
public static List applyIdentityTouserList(List UserList, Function funcuserTouser){
List userNameList=new ArrayList();
for(User user:UserList){
userNameList.add(funcuserTouser.apply(user));
}
return userNameList;
}
}
结果
User Name:Tike-one Age:13
User Name:Hello-world Age:25
User Name:Earth-one Age:10
User Name:Nanshou-one Age:15
User Name:Dogkeep-one Age:29
Function.identity():调用该静态方法,返回类名以及成员变量和值。
文档说明: