diff --git a/pkg/collections/array_list/array_list_methods.go b/pkg/collections/array_list/array_list_methods.go index b5f0c96..3146805 100644 --- a/pkg/collections/array_list/array_list_methods.go +++ b/pkg/collections/array_list/array_list_methods.go @@ -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 +}