HashMap
Inspect one language lane at a time so line-level text and code deltas stay readable.
Diff Lane
English
0 modified sections0 code block delta0 anchor delta
Diff Lane
中文
2 modified sections0 code block delta0 anchor delta
modifiedHashMaptext+1 line
v1.0.5
Section Text
1
使用 HashMap 类型需要导入 collection 包:2
3
<!-- run -->4
5
6
可以使用 HashMap 类型来构造元素为键值对的 Collection。7
8
HashMap 是一种哈希表,提供对其包含的元素的快速访问。表中的每个元素都使用其键作为标识,可以使用键来访问相应的值。9
10
仓颉使用 `HashMap<K, V>` 表示 HashMap 类型,K 表示 HashMap 的键类型,K 必须是实现了 Hashable 和 `Equatable<K>` 接口的类型,例如数值或 String。V 表示 HashMap 的值类型,V 可以是任意类型。11
12
13
元素类型不相同的 HashMap 是不相同的类型,所以它们之间不可以互相赋值。14
15
因此以下例子是不合法的。16
17
18
仓颉中可以使用构造函数的方式构造一个指定的 HashMap。19
20
<!-- run -->Code 1 · cangjie
1
import std.collection.*Code 2 · cangjie
1
var a: HashMap<Int64, Int64> = ... // HashMap whose key type is Int64 and value type is Int642
var b: HashMap<String, Int64> = ... // HashMap whose key type is String and value type is Int64Code 3 · cangjie
1
b = a // Type mismatchCode 4 · cangjie
1
let a = HashMap<String, Int64>() // Created an empty HashMap whose key type is String and value type is Int642
let b = HashMap<String, Int64>([("a", 0), ("b", 1), ("c", 2)]) // whose key type is String and value type is Int64, containing elements ("a", 0), ("b", 1), ("c", 2)3
let c = HashMap<String, Int64>(b) // Use another Collection to initialize a HashMap4
let d = HashMap<String, Int64>(10) // Created a HashMap whose key type is String and value type is Int64 and capacity is 105
let e = HashMap<Int64, Int64>(10, {x: Int64 => (x, x * x)}) // Created a HashMap whose key and value type is Int64 and size is 10. All elements are initialized by specified rule functionv1.1.0
Section Text
1
使用 HashMap 类型需要导入 collection 包:2
3
<!-- run -->4
5
6
可以使用 HashMap 类型来构造元素为键值对的 Collection。7
8
HashMap 是一种哈希表,提供对其包含的元素的快速访问。表中的每个元素都使用其键作为标识,可以使用键来访问相应的值。9
10
仓颉使用 `HashMap<K, V>` 表示 HashMap 类型,K 表示 HashMap 的键类型,K 必须是实现了 Hashable 和 `Equatable<K>` 接口的类型,例如数值或 String。V 表示 HashMap 的值类型,V 可以是任意类型。11
12
<!-- code_no_check -->13
14
15
元素类型不相同的 HashMap 是不相同的类型,所以它们之间不可以互相赋值。16
17
因此以下例子是不合法的。18
19
<!-- code_no_check -->20
21
22
仓颉中可以使用构造函数的方式构造一个指定的 HashMap。23
24
<!-- run -->Code 1 · cangjie
1
import std.collection.*Code 2 · cangjie
1
var a: HashMap<Int64, Int64> = ... // HashMap whose key type is Int64 and value type is Int642
var b: HashMap<String, Int64> = ... // HashMap whose key type is String and value type is Int64Code 3 · cangjie
1
b = a // Type mismatchCode 4 · cangjie
1
let a = HashMap<String, Int64>() // Created an empty HashMap whose key type is String and value type is Int642
let b = HashMap<String, Int64>([("a", 0), ("b", 1), ("c", 2)]) // whose key type is String and value type is Int64, containing elements ("a", 0), ("b", 1), ("c", 2)3
let c = HashMap<String, Int64>(b) // Use another Collection to initialize a HashMap4
let d = HashMap<String, Int64>(10) // Created a HashMap whose key type is String and value type is Int64 and capacity is 105
let e = HashMap<Int64, Int64>(10, {x: Int64 => (x, x * x)}) // Created a HashMap whose key and value type is Int64 and size is 10. All elements are initialized by specified rule functionmodified访问 HashMap 成员text+1 line
v1.0.5
Section Text
1
当需要对 HashMap 的所有元素进行访问时,可以使用 for-in 循环遍历 HashMap 的所有元素。2
3
需要注意的是,HashMap 并不保证按插入元素的顺序排列,因此遍历的顺序和插入的顺序可能不同。4
5
<!-- verify -->6
7
8
编译并执行上面的代码,有可能会输出:9
10
11
当需要知道某个 HashMap 包含的元素个数时,可以使用 size 属性获得对应信息。12
13
<!-- verify -->14
15
16
编译并执行上面的代码,会输出:17
18
19
当想判断 HashMap 中是否包含某个键时,可以使用 contains 函数。如果该键存在会返回 true,否则返回 false。20
21
<!-- run -->22
23
24
当想访问指定键对应的元素时,可以使用下标语法访问(下标的类型必须是键类型)。使用不存在的键作为索引会触发运行时异常。Code 1 · cangjie
1
import std.collection.HashMap2
3
main() {4
let map = HashMap<String, Int64>([("a", 0), ("b", 1), ("c", 2)])5
for ((k, v) in map) {6
println("The key is ${k}, the value is ${v}")7
}8
}Code 2 · text
1
The key is a, the value is 02
The key is b, the value is 13
The key is c, the value is 2Code 3 · cangjie
1
import std.collection.HashMap2
3
main() {4
let map = HashMap<String, Int64>([("a", 0), ("b", 1), ("c", 2)])5
if (map.size == 0) {6
println("This is an empty hashmap")7
} else {8
println("The size of hashmap is ${map.size}")9
}10
}Code 4 · text
1
The size of hashmap is 3Code 5 · cangjie
1
let map = HashMap<String, Int64>([("a", 0), ("b", 1), ("c", 2)])2
let a = map.contains("a") // a == true3
let b = map.contains("d") // b == falseCode 6 · cangjie
1
let map = HashMap<String, Int64>([("a", 0), ("b", 1), ("c", 2)])2
let a = map["a"] // a == 03
let b = map["b"] // b == 14
let c = map["d"] // Runtime exceptionsv1.1.0
Section Text
1
当需要对 HashMap 的所有元素进行访问时,可以使用 for-in 循环遍历 HashMap 的所有元素。2
3
需要注意的是,HashMap 并不保证按插入元素的顺序排列,因此遍历的顺序和插入的顺序可能不同。4
5
<!-- verify -->6
7
8
编译并执行上面的代码,有可能会输出:9
10
11
当需要知道某个 HashMap 包含的元素个数时,可以使用 size 属性获得对应信息。12
13
<!-- verify -->14
15
16
编译并执行上面的代码,会输出:17
18
19
当想判断 HashMap 中是否包含某个键时,可以使用 contains 函数。如果该键存在会返回 true,否则返回 false。20
21
<!-- run -->22
23
24
当想访问指定键对应的元素时,可以使用下标语法访问(下标的类型必须是键类型)。使用不存在的键作为索引会触发运行时异常。25
26
<!-- run.error -->Code 1 · cangjie
1
import std.collection.HashMap2
3
main() {4
let map = HashMap<String, Int64>([("a", 0), ("b", 1), ("c", 2)])5
for ((k, v) in map) {6
println("The key is ${k}, the value is ${v}")7
}8
}Code 2 · text
1
The key is a, the value is 02
The key is b, the value is 13
The key is c, the value is 2Code 3 · cangjie
1
import std.collection.HashMap2
3
main() {4
let map = HashMap<String, Int64>([("a", 0), ("b", 1), ("c", 2)])5
if (map.size == 0) {6
println("This is an empty hashmap")7
} else {8
println("The size of hashmap is ${map.size}")9
}10
}Code 4 · text
1
The size of hashmap is 3Code 5 · cangjie
1
let map = HashMap<String, Int64>([("a", 0), ("b", 1), ("c", 2)])2
let a = map.contains("a") // a == true3
let b = map.contains("d") // b == falseCode 6 · cangjie
1
let map = HashMap<String, Int64>([("a", 0), ("b", 1), ("c", 2)])2
let a = map["a"] // a == 03
let b = map["b"] // b == 14
let c = map["d"] // Runtime exceptions