tech.v3.datatype.jvm-map

Creation and higher-level manipulation for java.util.HashMap and java.util.concurrent.ConcurrentHashMap.

->bi-consumer

(->bi-consumer ifn)

Take a thing and return a bi-consumer. If already a bi-consumer return as is, else calls (bi-consumer x y (ifn x y))

->bi-function

(->bi-function ifn)

Take a thing and return a bi-function. If already a bi-function return as is, else calls (bi-function x y (ifn x y))

->function

(->function ifn)

Convert an ifn to a java.util.function.Function. If ifn is already a Function, return with no changes. Else calls (function x (ifn x))

add!

(add! set k)

Add a value to a set

bi-consumer

macro

(bi-consumer karg varg code)

Create a java.util.function.BiConsumer. karg will be the name of the first argument to the consumer, varg will be the name of the second. Code will be output inline into the consumer's accept function.

bi-function

macro

(bi-function karg varg code)

Create a java.util.function.BiFunction. karg will be the name of the first argument, varg will be the name of the second argument. code will be output inline into the function's apply function.

compute!

(compute! map k val-fn)

Compute a new value in-place. val-fn gets passed the key and existing value (if any). It is more efficient to pre-convert val-fn to a bi-function than to force this function to do it during execution.

compute-fn!

(compute-fn! map val-fn)

Given a map and a function to call if the value is in the map, return a function that takes a k and returns the result of calling (.compute map k (->bi-function val-fn)). val-fn gets passed the key and existing value (if any).

compute-if-absent!

(compute-if-absent! map k val-fn)

Compute a new value in-place. val-fn gets passed the key. It is more efficient to pre-convert val-fn to a java.util.function.Function than to force this function to do it during its execution.

compute-if-absent-fn!

(compute-if-absent-fn! map val-fn)

Given a map and a function to call if the value is not in the map, return a function that takes a k and returns the result of calling (.compute map k (->function val-fn)). val-fn gets passed the key when the value is not in the map.

compute-if-present!

(compute-if-present! map k val-fn)

Compute a new value in-place. val-fn gets passed the key and existing value. It is more efficient to pre-convert val-fn to a java.util.function.BiFunction than to force this function to do it during its execution.

compute-if-present-fn!

(compute-if-present-fn! map val-fn)

Given a map and a function to call if the value is in the map, return a function that takes a k and returns the result of calling (.computeIfPresent map k (->bi-function val-fn)). val-fn gets passed the existing value and new value is in the map.

concurrent-hash-map

(concurrent-hash-map)(concurrent-hash-map init)

concurrent-hash-map?

(concurrent-hash-map? item)

Return true if this is a concurrent hash map.

concurrent-set

(concurrent-set)

Create a concurrent-safe set. Wraps (.. (ConcurrentHashMap.) keySet)

contains?

(contains? item v)

Much faster version of contains? if you are dealing with java.util sets or maps.

entries

(entries map xform)(entries map)

Get the entries of the hashmap. Returns a randomly accessible list of data.

  • xform is a function from k, v to object. Defaults to vector.

entry-key

(entry-key v)

Given a java.util.Map$Entry, return the key

entry-value

(entry-value v)

Given a java.util.Map$Entry, return the value

foreach!

(foreach! item op & [parallel?])

Perform an operation for each entry in a map. Returns the op. This method will by default be serial unless parallel? is true in which case if the item in question is a concurrent hash map then the parallel version of foreach is used.

frequencies

(frequencies data)

Faster frequencies that returns either a hash map or concurrent hash map depending on if the sequence is iterable or if it can be converted to a reader.

For further use of this data see entries below.

function

macro

(function karg code)

Create a java.util.function.Function. karg will be the name of the argument, will be output inline into the function's apply method.

get

(get map k)(get map k default-value)

Get a value from a map with a potential default. Missing values will be nil otherwise.

hash-map

(hash-map)(hash-map init)

Create a java.util.HashMap. Note the only optional argument is for the initial size of the underlying hashtable.

Init can be of several forms:

  • number? - Used to initializes the hashtable size.
  • instance? java.util.Map - Hash map is copied.
  • sequential? - Used like 'into' without xform.

into-map!

(into-map! item data)

put a sequence of pairs into a jvm map.

merge-with!

(merge-with! lhs rhs op)

Merge rhs into lhs using op to resolve conflicts. Op gets passed two arguments, lhs-val rhs-val. If both rhs and lhs are concurrent hash maps then the merge is performed in parallel.

Returns lhs.

opt-map

(opt-map args)

Called from java. Ensure the keys of the map are keywords. Should be tested with (opt-map (object-array args)).

put!

(put! map k v)

Put a value into a map returning the map.

replace-all!

(replace-all! map val-fn)

Replace all the values in the map with new values computed with val-fn. val-fn gets passed the key and the existing value and must return a new value.