如何将 IntStream 映射到 Runnables 列表?

How to map an IntStream to a list of Runnables?

我正在尝试 return 使用 IntStreamn 空可运行列表,但我想我遗漏了一些东西:

public List<Runnable> getEmptyRunnables(int count) {
    return IntStream.rangeClosed(0, count)
       .mapToObj($ -> () -> {})  // IDE error: "target type of a lambda must be an interface" 
       .collect(Collectors.toList());
  }

我该如何进行这项工作?

调用方法时可以直接指定泛型类型mapToObj

IntStream.rangeClosed(0, 3)
   .<Runnable>mapToObj($ -> () -> {})
   .collect(Collectors.toList());
// or
List<Runnable> r = IntStream.rangeClosed(0, 3)
   .mapToObj($ -> (Runnable) () -> {})
   .collect(Collectors.toList());