feat: add ToMap for stream

This commit is contained in:
Pei PeiDong committed 2025-04-02 10:53:46 +08:00
1 parent a8a92844f3
commit 4947327ed6
3 files changed
+51

No files matched your search

+9
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
}