1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-13 01:02:28 +08:00

feat: add ToMap for stream

This commit is contained in:
Pei PeiDong
2025-04-02 10:53:46 +08:00
parent a8a92844f3
commit 4947327ed6
3 changed files with 51 additions and 0 deletions

View File

@@ -420,3 +420,12 @@ func (s Stream[T]) LastIndexOf(target T, equal func(a, b T) bool) int {
func (s Stream[T]) ToSlice() []T {
return s.source
}
func ToMap[T any, K comparable, V any](s Stream[T], mapper func(item T) (K, V)) map[K]V {
result := map[K]V{}
for _, v := range s.source {
k, v := mapper(v)
result[k] = v
}
return result
}