HashSet
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
中文
1 modified sections0 code block delta0 anchor delta
modifiedHashSettext+1 line
v1.0.5
Section Text
1
使用 HashSet 类型需要导入 collection 包:2
3
<!-- run -->4
5
6
可以使用 HashSet 类型来构造只拥有不重复元素的 Collection。7
8
仓颉使用 `HashSet<T>` 表示 HashSet 类型,T 表示 HashSet 的元素类型,T 必须是实现了 Hashable 和 `Equatable<T>` 接口的类型,例如数值或 String。9
10
11
元素类型不相同的 HashSet 是不相同的类型,所以它们之间不可以互相赋值。12
13
因此以下例子是不合法的。14
15
16
仓颉中可以使用构造函数的方式构造一个指定的 HashSet。17
18
<!-- run -->Code 1 · cangjie
1
import std.collection.*Code 2 · cangjie
1
var a: HashSet<Int64> = ... // HashSet whose element type is Int642
var b: HashSet<String> = ... // HashSet whose element type is StringCode 3 · cangjie
1
b = a // Type mismatchCode 4 · cangjie
1
let a = HashSet<String>() // Created an empty HashSet whose element type is String2
let b = HashSet<String>(100) // Created a HashSet whose capacity is 1003
let c = HashSet<Int64>([0, 1, 2]) // Created a HashSet whose element type is Int64, containing elements 0, 1, 24
let d = HashSet<Int64>(c) // Use another Collection to initialize a HashSet5
let e = HashSet<Int64>(10, {x: Int64 => (x * x)}) // Created a HashSet whose element type is Int64 and size is 10. All elements are initialized by specified rule functionv1.1.0
Section Text
1
使用 HashSet 类型需要导入 collection 包:2
3
<!-- run -->4
5
6
可以使用 HashSet 类型来构造只拥有不重复元素的 Collection。7
8
仓颉使用 `HashSet<T>` 表示 HashSet 类型,T 表示 HashSet 的元素类型,T 必须是实现了 Hashable 和 `Equatable<T>` 接口的类型,例如数值或 String。9
10
<!-- code_no_check -->11
12
13
元素类型不相同的 HashSet 是不相同的类型,所以它们之间不可以互相赋值。14
15
因此以下例子是不合法的。16
17
<!-- code_no_check -->18
19
20
仓颉中可以使用构造函数的方式构造一个指定的 HashSet。21
22
<!-- run -->Code 1 · cangjie
1
import std.collection.*Code 2 · cangjie
1
var a: HashSet<Int64> = ... // HashSet whose element type is Int642
var b: HashSet<String> = ... // HashSet whose element type is StringCode 3 · cangjie
1
b = a // Type mismatchCode 4 · cangjie
1
let a = HashSet<String>() // Created an empty HashSet whose element type is String2
let b = HashSet<String>(100) // Created a HashSet whose capacity is 1003
let c = HashSet<Int64>([0, 1, 2]) // Created a HashSet whose element type is Int64, containing elements 0, 1, 24
let d = HashSet<Int64>(c) // Use another Collection to initialize a HashSet5
let e = HashSet<Int64>(10, {x: Int64 => (x * x)}) // Created a HashSet whose element type is Int64 and size is 10. All elements are initialized by specified rule function