import java.util.Arrays;
/**
* 函数式编程的不变模式
*/
public class ArrStream {
public static void main(String[] args){
int[] arr = {1,2,3,4,5};
Arrays.stream(arr).map((x)->x+1).forEach(System.out::print);
System.out.println();
Arrays.stream(arr).forEach(System.out::print);
}
//23456
//12345
}
/**
* FunctionalInterface注解
*/
@FunctionalInterface //用于表明是一个函数式接口
public interface IntHandler {
void handle(int i);//只包含一个抽象方法
}
@FunctionalInterface //用于表明是一个函数式接口
public interface IntHandler {
boolean equals(Object obj);//此时编译未通过,不是函数式接口,因为equals()方法在java.lang.Object中已经实现
}
/**
* 符合函数式接口要求
*/
@FunctionalInterface //用于表明是一个函数式接口
public interface IntHandler {
boolean equals(Object obj);
void handle(int i);
}
public interface IHorse {
void eat();
//使用default关键字,可以在接口内定义实例方法
default void run(){
System.out.println("Horse run");
}
}
public interface IAnimal {
default void breath(){
System.out.println("breath");
}
}
public interface IDonkey {
void eat();
default void run(){
System.out.println("Donkey run");
}
}
/**
* 同时拥有不同接口的实现方法
*/
public class Mule implements IAnimal,IHorse,IDonkey {
@Override
public void eat() {
System.out.println("Mule eat");
}
@Override
public void run(){
IHorse.super.run();
}
public static void main(String[] args){
Mule mule = new Mule();
mule.run();
mule.breath();
}
//Horse run
//breath
}
原文:https://www.cnblogs.com/fly-book/p/11492471.html