Idiom #8 Create a map (associative array)
Create a new map object x, and provide some (key, value) pairs as initial content.
- Ada
- C
- Caml
- Clojure
- C++
- C++
- C#
- C#
- D
- Dart
- Elixir
- Elixir
- Erlang
- Go
- Haskell
- JS
- JS
- JS
- Java
- Java
- Java
- Java
- Java
- Kotlin
- Kotlin
- Kotlin
- Lisp
- Lua
- Obj-C
- PHP
- Pascal
- Perl
- Python
- Python
- Ruby
- Rust
- Rust
- Scala
- Scheme
- Smalltalk
- VB
declare
package Maps is new Indefinite_Hashed_Maps (Key_Type => String,
Element_Type => Integer,
Hash => Ada.Strings.Hash,
Equivalent_Keys => "=");
use Maps;
X : Map := Empty_Map;
begin
X.Insert ("One", 1);
X.Insert ("Two", 2);
X.Insert ("Three", 3);
end;
ENTRY a = {"foo", "twenty"};
ENTRY b = {"bar", "three"};
if (hcreate (23)) {
hsearch(a, ENTER);
hsearch(b, ENTER);
}
This POSIX functions maintain a single global hashmap. The GNU C library provides hcreate_r
var x = new Dictionary<string, int> {
["year"] = 2019,
["month"] = 12
};
const x = new Map([["one",1],["two",2]]);
The Map constructor can take an array of [key, value] pairs.
Map<String, Integer> x = of("x", 1, "y", 2);
Immutable
Map<String,Integer> x = new HashMap<>();
x.put("one", 1);
x.put("two", 2);
import static java.util.Map.entry;
import static java.util.Map.ofEntries;
import java.util.Map;
import java.util.Map.Entry;
Entry<String, Integer> a = entry("x", 1),
b = entry("y", 2);
Map<String, Integer> x = ofEntries(a, b);
Map<String, Integer> x = new HashMap<>(of("x", 1, "y", 2));
Mutable
type TMap = specialize TFPGMap<String, Integer>;
var x: TMap;
begin
x := TMap.Create;
x['one'] := 1;
x['two'] := 2;
end.
my %x = (
name => 'Roboticus',
'foo bar' => 'joe'
);
The '=>' operator autoquotes the word on its left if it begins with a letter or underscore and is composed only of letters, digits and underscores. Otherwise quotes are required.
let mut x = BTreeMap::new();
x.insert("one", 1);
x.insert("two", 2);
Something different than a BTreeMap might be used, depending on the usage.
The function new of the type BTreeMap returns a usable map.
The map is stored mutable in the variable x.
Maps in Rust are generic and type safe at compile time.
The function new of the type BTreeMap returns a usable map.
The map is stored mutable in the variable x.
Maps in Rust are generic and type safe at compile time.
(define x '(
("one" 1)
("two" 2)
("three" 3)))
Warning : the built-in association lists are inefficient, so don't use them for large data sets.
x := Dictionary newFrom: {
#a -> 1.
#b -> Object new}.
Alternate: { #a -> 1. #b -> Object new} asDictionary