Clone wiki

bolts / Collections

Home * На русском


Collections in bolts extend JDK collection interfaces. For each Xxx JDK collection interface there is XxxF bolts interface:

interface IterableF extends Iterable { ... }
interface CollectionF extends Collection { ... }
interface ListF extends List { ... }
interface SetF extends Set { ... }
interface MapF extends Map { ... }
interface IteratorF extends Iterator { ... }

Handy Collection Interface Methods

IterableF.mkString(String separator) — joins collection elements to String using specified separator

CollectionF.unique() — collects elements to Set

CollectionF.toList() — collects elements to List

CollectionF.sort* — sort collection

ListF.first, last — get first, last list item

ListF.take(count), drop(count) — take first or last elements. Unlike subList() method, these methods do not throw

ListF.reverse() — reverse list

Construct Collection Instances

CollectionsF class — primary collection construction utility. It has many static methods.

To avoid typing CollectionsF each time you need to create collection, bolts has Cf short alias. Cf can be used instead of CollectionsF.

... by Specifying Items

Cf.<String>list(); // empty list
Cf.list(1, 2, 3); // List with items 1, 2, 3
Cf.set("a", "b"); // Set with items "a", "b"

Explicit type parameter of "list" method is rarely needed.

... by Wrapping Existing Collection

There are several static x methods in Cf, each of them takes regular Xxx collection and produces its wrapper of type XxxF. Example:

Cf.x(new ArrayList<String>());

returns ArrayList wrapper, that has ListF interface.

... Standard JDK Collection Wrappers

arrayList() — returns a list, that behaves likeArrayList — i. e. it is mutable, and has constant-time element access.

hashSet() —SetF, that behaves like HashSet.

There are also treeSet, concurrentHashSet, hashMap, treeMap, concurrentHashMap, identityHashSet, identityHashMap, linkedList methods.


Source repository has several examples.

Functional Operations

Functional collection operations are described on another page.