What is the get method of a map in Clojure?

Overview

A map is a collection that assigns keys to values. The get method is used to get the element of a collection by its key.

Syntax

(get hmap key)
map get method

Parameters

The get method receives two parameters:

  1. hmap: The hash key and value
  2. key: The keys used to get the value

Return value

This method returns the value of the key passed.

Example

(ns clojure.examples.example
(:gen-class))
(defn gett []
(def keyss (hash-map "z" "1" "b" "22" "f" "53"))
(println keyss)
(println (get keyss "z")))
(gett)

Explanation

  • Line 3: We define a gett function.
  • Line 4: We define a variable, keyss, using the def keyword. Next, we give it the collection.(hashmap "z" "1" "b" "22" "f" "53").
  • Line 5: We print the collection that contains both the keys and the value using the println. The get method is used to get the data value using key.
  • Line 6: We use the println to print the value returned by the key z.
  • Line 7: We call the gett function.

Free Resources