feature: added filter method to array list

master
amorozov 2024-06-25 13:36:47 +03:00
parent 85f15eee46
commit 574f36e64e
1 changed files with 18 additions and 0 deletions

View File

@ -208,3 +208,21 @@ func (self *ArrayList[T]) Find(filter func(T) bool) *optional.Optional[T] {
return optional.Empty[T]()
}
func (self *ArrayList[T]) Filter(filter func(T) bool) *ArrayList[T] {
return self.FilterTo(filter, NewArrayList[T]())
}
func (self *ArrayList[T]) FilterTo(filter func(T) bool, resultList *ArrayList[T]) *ArrayList[T] {
if resultList == nil {
panic(fmt.Errorf("can not filter %v to nil list", self))
}
self.ForEach(func(value T) {
if filter(value) {
resultList.Add(value)
}
})
return resultList
}