1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 23:22: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

@@ -412,3 +412,22 @@ func ExampleStream_LastIndexOf() {
// -1
// 3
}
func ExampleStream_ToMap() {
type Person struct {
Name string
Age int
}
s := FromSlice([]Person{
{Name: "Tom", Age: 10},
{Name: "Jim", Age: 20},
{Name: "Mike", Age: 30},
})
m := ToMap(s, func(p Person) (string, Person) {
return p.Name, p
})
fmt.Println(m)
// Output:
// map[Jim:{Jim 20} Mike:{Mike 30} Tom:{Tom 10}]
}