PHPackages                             b2pweb/bdf-collections - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. b2pweb/bdf-collections

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

b2pweb/bdf-collections
======================

Collections implementations with java 8 stream like api.

v1.1.8(5mo ago)29.1k↑60%3MITPHPPHP ~7.1 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0CI failing

Since Jul 12Pushed 5mo ago2 watchersCompare

[ Source](https://github.com/b2pweb/bdf-collections)[ Packagist](https://packagist.org/packages/b2pweb/bdf-collections)[ RSS](/packages/b2pweb-bdf-collections/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (11)Used By (3)

Collections
===========

[](#collections)

Implementation of commons collections, with java-like stream to apply transformations.

[![build](https://github.com/b2pweb/bdf-collections/actions/workflows/php.yml/badge.svg)](https://github.com/b2pweb/bdf-collections/actions/workflows/php.yml)[![codecov](https://camo.githubusercontent.com/7f6c562027a0ea67282233b727c06f148a5d530c0a87b7e7ee9a313a32b333dd/68747470733a2f2f636f6465636f762e696f2f6769746875622f6232707765622f6264662d636f6c6c656374696f6e732f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d564f4653504557594b58)](https://app.codecov.io/github/b2pweb/bdf-collections)[![Packagist Version](https://camo.githubusercontent.com/5f12bbe7cb14a67ba50b64a4dac03cd0aa1ee0d4059dea6230bc30474c038f48/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6232707765622f6264662d636f6c6c656374696f6e732e737667)](https://packagist.org/packages/b2pweb/bdf-collections)[![Total Downloads](https://camo.githubusercontent.com/701f9b2c139a9565e7023fa995fd0830600a22fd3215be6140d3516366cdd08e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6232707765622f6264662d636f6c6c656374696f6e732e737667)](https://packagist.org/packages/b2pweb/bdf-collections)[![Type Coverage](https://camo.githubusercontent.com/9b42d2647cc6e2a5077c257f6f2fc1a400542d2b8dfb9e6e9c4872e678a0b604/68747470733a2f2f73686570686572642e6465762f6769746875622f6232707765622f6264662d636f6c6c656374696f6e732f636f7665726167652e737667)](https://shepherd.dev/github/b2pweb/bdf-collections)

- [Installation](#installation-with-composer)
- [Usage](#usage)
    - [Collections](#collections-1)
        - [ArrayCollection](#arraycollection)
        - [OrderedCollection](#orderedcollection)
        - [HashSet](#hashset)
        - [HashTable](#hashtable)
    - [Streams](#streams)
        - [Usage](#usage-1)
        - [MutableArrayStream](#mutablearraystream)
    - [Optional](#optional)

Installation with composer
==========================

[](#installation-with-composer)

```
composer require b2pweb/bdf-collections

```

Usage
=====

[](#usage)

Collections
-----------

[](#collections-1)

All collections implements the `Bdf\Collection\CollectionInterface` interface.

A collection is a simple bag of elements, with a restricted set of methods :

- `add(mixed $element)` Add the element to the collection. The implementation may reject the operation, and returns false.
- `addAll(iterator $elements)` Equivalent to `foreach ($elements as $element) { $collection->add($element); }`.
- `clear()` Remove all elements.
- `replace(iterable $elements)` Clear and replace all elements.
- `empty()` Check if the collection has no elements.
- `contains($element, bool $strict = false)` Check if the collection contains the given element. If `$strict` is set to `true`, use strict comparison operator `===`.
- `forEach(callable $callback)` Iterates over all elements, using a callback.
- `toArray()` Convert the collection to an array.
- And inherited methods of `IteratorAggregate`, `Countable` and `Streamable`

The base behavior of collections is extended by other interfaces :

- `OrderedCollectionInterface` : Ensure that all elements of the collection are ordered. Add (or modify) methods :
    - `contains($element, bool $strict = false)` Perform a binary search. The complexity of the call is O(log(n)) instead of O(n) on a simple collection.
    - `search($element, bool $strict = false)` Get the element position. Works like `contains($element, bool $strict = false)` but return the position instead of `true`. The expression `$element == $collection->at($collection->search($element))` is always `true` when `$element` exists.
    - `at(int $position)` Get an element at the given position.
    - `ArrayAccess` methods, expects `offsetSet()`. Works with the position as offset.
- `SetInterface` : Ensure that the collection no not contains any duplicated elements. Add (or modify) methods :
    - `add($element)` If the collection already contains the element, will return false, ignore the operation.
    - `addAll(iterable $elements)` Return `false` is at least on element is already added.
    - `lookup($element)` Find the corresponding elements stored into the Set.
- `TableInterface` Add the key handling for modifying, or accessing elements :
    - `set($key, $value)` Set a value at the given key.
    - `get($key)` Get a value at the given key.
    - `hasKey($key)` Check if a key exists.
    - `keys()` Get all keys of the table.
    - `values()` Get all values as array. This is equivalent to `iterator_to_array($collection)`
    - `forEach(callable $callback)` Iterates over elements, but add the key as second argument of the callback.
    - `ArrayAccess` methods

### ArrayCollection

[](#arraycollection)

The `Bdf\Collection\ArrayCollection` is the base implementation of `TableInterface` using an inner PHP array. It has great performances but do not handle complex key types, or optimised search.

Use as collection :

```
