public class LambdaTest{
public static void main(String[] args) {
String[] str = {"aa","bb","cc","dd"};
LambdaTest t = new LambdaTest();
t.test2(str);
}
/**
* 匿名内部实现数组遍历
* @param str
*/
public void test(String[] str) {
ArrayInterfaceImpl arrayInterfaceImpl = new ArrayInterfaceImpl();
arrayInterfaceImpl.getInfo(str, new ArrayInterface() {
@Override
public void showInfo(String[] str) {
// TODO Auto-generated method stub
for(String s:str) {
System.out.println(s+"匿名内部类");
}
}
});
}
/**
* 之前的Lambda表达式实现数组遍历
* @param str
*/
public void test1(String[] str) {
ArrayInterfaceImpl arrayInterfaceImpl = new ArrayInterfaceImpl();
arrayInterfaceImpl.getInfo(str, (String[] target) -> {
for(String s:target) {
System.out.println(s+"Lambda表达式");
}
});
}
/**
* jdk1.8 Lambda表达式实现数组遍历
* @param str
*/
public void test2(String[] str) {
ArrayInterfaceImpl arrayInterfaceImpl = new ArrayInterfaceImpl();
arrayInterfaceImpl.getInfo(str, (strs)->{
System.out.println(Arrays.toString(strs));
});
}
interface ArrayInterface{
public void showInfo(String[] str);
}
class ArrayInterfaceImpl{
public void getInfo(String[] str,ArrayInterface arrayInterface) {
arrayInterface.showInfo(str);
}
}
}