public interface Study{ void study();}public class Main{ public static void main(String[] args){ // 使用匿名内部类 Study study = new Study(){ @Override public void study(){ System.out.println("study"); } } // 使用 lambda Study study02 = () -> System.out.println("study"); }}
public interface Study{ void study(int a);}public class Main{ public static void main(String[] args){ Study study = (int a)->{ return a; } // 继续省略 Study study2 = a -> { return a; } }}
注意,如果方法体中只有一个返回语句,可以直接省去花括号和return关键字:
Study study = a -> a;
在Java 8之前,我们在某些情况下可能需要用到匿名内部类,比如:
public static void main(String[] args) { //现在我们想新建一个线程来搞事情 Thread thread = new Thread(new Runnable() { //创建一个实现Runnable的匿名内部类 @Override public void run() { //具体的实现逻辑 System.out.println("Hello World!"); } }); thread.start();}
@FunctionalInterfacepublic interface Test { String test(Integer i);}public class Main { public static void main(String[] args) { Test test = Main::impl; } public static String impl(Integer i){ // 现在有一个静态方法,刚好匹配接口中抽象方法的返回值和参数列表 return "我是已经存在的实现"+i; }}
比如我们现在需要对一个数组进行排序:
public class ArraySortExample { public static void main(String[] args) { Integer[] array = new Integer[]{4, 6, 1, 9, 2, 0, 3, 7, 8, 5}; //来个数组 Arrays.sort(array, new Comparator<Integer>() { //Arrays.sort()可以由我们自己指定排序规则,只需要实现Comparator方法即可 @Override public int compare(Integer o1, Integer o2) { return o2 - o1; } }); System.out.println(Arrays.toString(array)); //按从小到大的顺序排列 }}
但是我们发现,Integer类中有一个叫做compare的静态方法:
public static int compare(int x, int y) { return (x < y) ? -1 : ((x == y) ? 0 : 1);}
三元运算符
int a = 10;int b = 20;int min = (a < b) ? a : b; // 如果 a < b,就返回 a,否则返回 bSystem.out.println(min); // 输出 10
public int compare(Integer o1, Integer o2) { return o1 - o2;}
其中o1和o2都是Integer类型的,我们发现Integer类中有一个compareTo方法:
public int compareTo(Integer anotherInteger) { return compare(this.value, anotherInteger.value);}
只不过这个方法并不是静态的,而是对象所有:
compareTo: Compares two Integer objects numerically.
Params: anotherInteger – the Integer to be compared.
Returns:
the value 0 if this Integer is equal to the argument Integer; a value less than 0 if this Integer is numerically less than the argument Integer; and a value greater than 0 if this Integer is numerically greater than the argument Integer (signed comparison).
传统写法:
Integer[] array = new Integer[]{4, 6, 1, 9, 2, 0, 3, 7, 8, 5};Arrays.sort(array, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o1.compareTo(o2); //这样进行比较也行,和上面效果依然是一样的 }});System.out.println(Arrays.toString(array));
// jdk 21, Iterable接口的 forEach源码default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } }
ArrayList 重写了forEach()
@Overridepublic void forEach(Consumer<? super E> action) { Objects.requireNonNull(action); final int expectedModCount = modCount; final Object[] es = elementData; final int size = this.size; for (int i = 0; modCount == expectedModCount && i < size; i++) action.accept(elementAt(es, i)); if (modCount != expectedModCount) throw new ConcurrentModificationException(); }