Kotlin标准库中有哪些Java 8 Stream.collect的类似功能?
你好!我正在从Java转向Kotlin,并积极学习标准库。在Java中,我经常使用Stream API,特别是带有各种collectors的collect方法:Collectors.toList()、Collectors.groupingBy()、Collectors.joining()等。
例如,在Java中我可以这样写:
List<String> names = people.stream()
.filter(p -> p.getAge() > 18)
.map(Person::getName)
.collect(Collectors.toList());
或者按年龄对人进行分组:
Map<Integer, List<Person>> byAge = people.stream()
.collect(Collectors.groupingBy(Person::getAge));
在Kotlin中,我看到有.filter、.map函数,但找不到.collect(Collectors.toList())的直接类似功能。我尝试过写.toList(),但不确定这是否正确且高效。
请问,在标准库中有哪些函数可以实现类似功能?