【发布时间】:2015-01-12 00:26:31
【问题描述】:
我错过了什么?为什么我必须在下面使用Object::toString 而不是Integer::toString?它与泛型的类型擦除有什么关系吗?
Arrays.asList(1,2,3).stream().map(Integer::toString).forEach(System.out::println); //Won't compile
Arrays.asList(1,2,3).stream().map(Object::toString).forEach(System.out::println); //Compiles and runs fine
【问题讨论】:
-
顺便说一句,你可以写
Arrays.asList(1,2,3).forEach(System.out::println);或Stream.of(1,2,3).forEach(System.out::println);或IntStream.rangeClosed(1, 3).forEach(System.out::println);
标签: java java-8 method-reference