liveBook · Manning

Contains

As explained in Clojure: The Essential Reference

.contains is a Java Collection or String method that tests whether a collection contains an element or a string contains a substring.

Use `.contains` when you need to test whether a Clojure vector contains a value or a string contains a substring. It calls an underlying Java method; it is not the same operation as Clojure’s `contains?`. section 8.2.3 section 12.1

Values, not vector indexes

For a vector, `.contains` compares values, while `contains?` checks whether an index is present: ```clojure (.contains [1 2 :a :b] :a) ; => true (contains? [1 2 :a :b] :a) ; => false (.contains [1 2 :a :b] 3) ; => false (contains? [1 2 :a :b] 3) ; => true ``` section 12.1

Strings and maps

A string’s `.contains` checks for a substring. Do not assume the same method is available on every collection: the map call below fails. For map keys, use `contains?` instead. ```clojure (.contains "somelongstring" "long") ; => true (.contains {:a 1} :a) ; => IllegalArgumentException (contains? {:a 1} :a) ; => true ``` section 8.2.3

How Clojure: The Essential Reference covers it

How deep it goes
Shows calls and results, including an unsupported map call, without detailing Java method implementations.
Examples
Compares value and index checks on a vector, then tests a string substring and an unsupported map call.
What you can do after
Apply it via Java interop to check vector values or string substrings without confusing the result with `contains?`.
How it connects
Contrasts Java interop with `contains?`, vector indexing, map-key lookup, and string substring search.

Also explained in

Compare how 2 books explain contains →

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.