Extending JDK 8 Collections
cyclops-react adds a series of extensions to standard (mutable) Java collections as well as support for pCollections, a set of persistent collections that also implement the JDK interfaces. See
An Extended List (ListX)
For example to create an extended List we can use the ListX.of (values) or ListX.fromIterable(iterable) creational methods.
ListX implements FluentCollectionX (as well as FluentSequenceX, FluentCollectionX, List & Colleciton). FluentCollectionX adds fluent analogues to JDK manipulation methods such as add / remove - these analogues use plus / minus instead and return ListX so methods can be chained.
Available JDK Extensions
Cyclops provides the following extensions for standard JDK Collections
Each CollectionX implementation adds a large array of additional operators to the collection.
By default JDK extensions are mutable, and are still mutable after each operation is applied. It is possible to construct an immutable view for each collection instead. In the example below the calls to plus and minus result in an UnsupportedOperationException.
We do this by making use of an Immutable Collector instance. Cyclops provides one per collection extension.
We’ve also added support for persistent collections, persistent collections are functional data structures that preserve previous versions on ‘update’. This allows more efficient manipulation of immutable collections than standard imperative implementations allow. Cyclops also provides extensions for the pCollections library - from the website.
PCollections serves as a persistent and immutable analogue of the Java Collections Framework. This includes efficient, thread-safe, generic, immutable, and persistent stacks, maps, vectors, sets, and bags, compatible with their Java Collections counterparts.
Available PCollection extensions are
- PStackX : javadoc : persistent analogue of JDK LinkedList (implements List)
- PVectorX : javadoc : persistent analogue of JDK ArrayList (implements List)
- PSetX : javadoc : persistent analogue of JDK Set (implements Set)
- POrderedSetX : javadoc : persistent analogue of JDK SortedSet (implements Set)
- PBagX : javadoc :unordered persistent colleciton that supports duplicates
- PQueueX : javadoc : persisent Queue (implements Queue)
Differences with java.util.stream.Stream
An important difference with Stream is that all operations are applied eagerly. Therefore for long chains of operations converting the extended colllection to a Stream and then converting back will give better performance.