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:
@@ -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}]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user