| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"github.com/emirpasic/gods/v2/maps/treemap" |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
func main() { |
| 16 |
m := treemap.New[int, string]() |
| 17 |
m.Put(0, "a") |
| 18 |
m.Put(1, "b") |
| 19 |
m.Put(2, "c") |
| 20 |
it := m.Iterator() |
| 21 |
|
| 22 |
fmt.Print("\nForward iteration\n") |
| 23 |
for it.Next() { |
| 24 |
key, value := it.Key(), it.Value() |
| 25 |
fmt.Print("[", key, ":", value, "]") |
| 26 |
} |
| 27 |
|
| 28 |
fmt.Print("\nForward iteration (again)\n") |
| 29 |
for it.Begin(); it.Next(); { |
| 30 |
key, value := it.Key(), it.Value() |
| 31 |
fmt.Print("[", key, ":", value, "]") |
| 32 |
} |
| 33 |
|
| 34 |
fmt.Print("\nBackward iteration\n") |
| 35 |
for it.Prev() { |
| 36 |
key, value := it.Key(), it.Value() |
| 37 |
fmt.Print("[", key, ":", value, "]") |
| 38 |
} |
| 39 |
|
| 40 |
fmt.Print("\nBackward iteration (again)\n") |
| 41 |
for it.End(); it.Prev(); { |
| 42 |
key, value := it.Key(), it.Value() |
| 43 |
fmt.Print("[", key, ":", value, "]") |
| 44 |
} |
| 45 |
|
| 46 |
if it.First() { |
| 47 |
fmt.Print("\nFirst key: ", it.Key()) |
| 48 |
fmt.Print("\nFirst value: ", it.Value()) |
| 49 |
} |
| 50 |
|
| 51 |
if it.Last() { |
| 52 |
fmt.Print("\nLast key: ", it.Key()) |
| 53 |
fmt.Print("\nLast value: ", it.Value()) |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
seek := func(key int, value string) bool { |
| 58 |
return strings.HasSuffix(value, "b") |
| 59 |
} |
| 60 |
|
| 61 |
it.Begin() |
| 62 |
for found := it.NextTo(seek); found; found = it.Next() { |
| 63 |
fmt.Print("\nNextTo key: ", it.Key()) |
| 64 |
fmt.Print("\nNextTo value: ", it.Value()) |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
it.End() |
| 73 |
for found := it.PrevTo(seek); found; found = it.Prev() { |
| 74 |
fmt.Print("\nNextTo key: ", it.Key()) |
| 75 |
fmt.Print("\nNextTo value: ", it.Value()) |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
} |