如果 forEach 是抽象方法,它如何将 lambda 表达式应用于数组?

How does forEach apply the lambda expression to an array if it's an abstract method?

List<String> friends = Arrays.asList("Brian", "Nate", "Neal", "Sara");
friends.forEach(name -> System.out.println(name));

我正在研究java,我发现了这个例子,我只是把它放在一个main中,它正常打印数组,所以我的问题是:

在我的理解中name -> System.out.println(name)是一个lambda表达式,用来快速实现一个打印功能,但是我想知道forEach是如何把它应用到数组的每一个元素上的?它是Iterable 接口,因此...为空,那么告诉将打印应用于数组中的每个名称的代码在哪里?

forEach 是来自 Iterable 接口的默认方法,默认实现只是针对每个元素

的循环调用 Consumer 进行了增强
for (T t : this)
     action.accept(t);

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. Unless otherwise specified by the implementing class, actions are performed in the order of iteration (if an iteration order is specified). Exceptions thrown by the action are relayed to the caller.

Arrays.asList returns ArrayList 在当前的实现中,它有一个 forEach 的实现,用普通的 for-loop 迭代,将 lambda 应用于每个元素.它覆盖(通过一些继承层)并实现 Iterable.forEach。当然,它也扩展了 List。来自 java.util.ArrayList:

for (int i = 0; modCount == expectedModCount && i < size; i++)
    action.accept(elementAt(es, i)); 

来自java.util.Arrays:

public static <T> List<T> asList(T... a) {
    return new ArrayList<>(a);
}