Код: Выделить всё
// works, of course
List list = List.of(new C1());
List list = List.of(new C1()).stream().toList();
// works, too, but why, considering the ones below that don't?
List list = List.of(new C1());
// doesn't compile, why?
List list = List.of(new C1()).stream().toList();
List list = List.of(new C2()).stream().toList();
// but this works, why?
List list = List.of(new C2()).stream().collect(Collectors.toList());
// works, again why?
List list = new ArrayList();
List.of(new C1()).stream().forEach(c1 -> list.add(c1)).toList();
// works
List list = List.of(new C1(), new C2()).stream().toList();
< /code>
Я не понимаю, почему строки 4, 5 не компилируются, пока это делают 3, 6, 7. Оба должны компилировать, или нет. Например: < /p>
private Stream aa() {
return Stream.of(new C1()); // works
return List.of(new C1()).stream() // doesn't
return List.of(new C1()).stream().map(a->a) // works, why?
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... tinterface