liveBook · Manning

Collection

As explained in Clojure: The Essential Reference

A Java collection is an object from Java's collection abstractions that stores or organizes multiple values.

A collection holds multiple values for operations such as counting, access, traversal, and transformation. Clojure’s collection functions work across its own data structures, and many also accept Java collections or arrays. That interoperability lets Java-provided data enter ordinary collection-processing code, though each function has its own input contract. section 8.5 section 8.0

Check the operation’s contract rather than assuming every input behaves alike:

  • `count` returns the number of elements in a collection; `(count nil)` is `0`. What counts as an element depends on the type: map entries are counted as tuples, while record fields are counted as fields. Scalars such as numbers cannot be counted. section 8.1.2
  • `set` accepts a collection, including common Java iterable types, and produces a Clojure set with duplicates removed. Empty or nil input produces an empty set; non-collection input raises `IllegalArgumentException`. section 13.2
  • Performance depends on the input structure. Counting a vector is O(1), while counting a lazy sequence is O(n). A polymorphic function is convenient, but its cost is not necessarily uniform. section 8.1.2 section 8.0

Count values or remove duplicates

(count [1 2 3]) ;; 3 (set [1 2 3 4 1 4]) ;; #{1 4 3 2} Use `count` when a decision depends on size, such as checking command-line arguments. Use `set` when subsequent work needs distinct values; `contains?` can then test membership. The same `set` operation can consume supported Java iterable inputs. section 8.1.2 section 13.2

Collections are the broad container abstraction; sequences are a more specialized abstraction. Standard functions often bridge the two, so choose an operation for the job, then check its accepted inputs and any type-specific cost. section 8.5 section 8.3

How Clojure: The Essential Reference covers it

How deep it goes
Explains `count` and `set` contracts and selected performance differences, without detailing Java collection interfaces.
Examples
Uses vector counting, duplicate removal with `set`, and command-line argument checking; provides no concrete Java collection example.
What you can do after
Apply it via Clojure’s `count` and `set` to inspect or transform supported collections, including common Java iterable inputs.
How it connects
Connects collections to sequences, arrays, `count`, `set`, and Java interoperability.

Also explained in

Compare how 16 books explain collection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.