statmark56
11/10/2023, 3:25 PMval map: Map<Int, String> = ...
If I create a list via:
val list: List<String> = map.values.toList()
Will I cause memory allocation: 2*N, where N = size of String entries (1 from map, 1 from the list)?Jacob
11/10/2023, 3:49 PMJohann Pardanaud
11/10/2023, 3:51 PMKlitos Kyriacou
11/10/2023, 4:03 PMmap.values is a collection. There's no technical reason asList couldn't exist for a collection, but it doesn't. It's only defined for arrays.Johann Pardanaud
11/10/2023, 4:04 PMJohann Pardanaud
11/10/2023, 4:05 PMKlitos Kyriacou
11/10/2023, 4:06 PMmap.values is a Collection, which is already an Iterable.
The main point for the OP is don't call toList() unless you absolutely need a List.Johann Pardanaud
11/10/2023, 4:08 PMstatmark56
11/10/2023, 4:13 PMWout Werkman
11/10/2023, 4:19 PMMap. Map.values will most likely be implemented as a view. So it will most likely have allocations independent of N.
.toList() will always introduce at least N, and most likely not moreJacob
11/10/2023, 4:20 PMephemient
11/10/2023, 4:51 PMilya.gorbunov
11/12/2023, 12:41 AMThat could be nice, but map.values is a collection. There's no technical reason@Klitos Kyriacou For example, one reason is thatcouldn't exist for a collection,asList
List requires implementing indexed access to elements and reverse iteration and Collection doesn't provide these.Klitos Kyriacou
11/12/2023, 3:03 PM