What are the analogs of Java 8 Stream.collect in the Kotlin standard library?
Hi everyone! I'm switching from Java to Kotlin and ran into a problem. In Java, I constantly used Stream API and the `collect()` method for working with collections. For example, to group a list of users by city or collect into a Map, I wrote:
```java
List users = getUsers();
Map> byCity = users.stream()
.collect(Collectors.groupingBy(User::getCity));
```
Or to find unique names and sort them:
```java
List uniqueSortedNames = users.stream()
.map(User::getName)
.distinct()
.sorted()
.collect(Collectors.toList());
```
In Kotlin, I see that there are `map`, `filter`, `sorted` functions, but I can't find a direct analog of `collect()`. I know that in Kotlin collections are extended with functions like `toList()`, `toSet()`, `g
No answers yet.
Log in to leave an answer
Blogs