feature: added filter method to array list
parent
85f15eee46
commit
574f36e64e
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue