Set是一種具有下列特性的項目集合:
用Set()建構函式建立,並以任何疊代器作為參數。
julia> s1 = Set(1:4)
Set{Int64} with 4 elements:
4
2
3
1
用push!()加入新元素(和陣列一樣),並用delete!()移除元素。
julia> push!(s1, 5)
Set{Int64} with 5 elements:
5
4
2
3
1
# Duplicates are ignored
julia> push!(s1, 3)
Set{Int64} with 5 elements:
5
4
2
3
1
julia> delete!(s1, 5)
Set{Int64} with 4 elements:
4
2
3
1
julia> length(s1) # length counts entries, despite the non-sequential type
4
和幾種其他集合型態一樣,用in或∈運算子檢查元素是否屬於集合(輸入\in後按 Tab 鍵即可打出這個符號)。
julia> 3 ∈ s1
true
以下是對成對集合支援的運算(括號內顯示的是運算子符號的輸入快速鍵)。
union(A, B)或A ∪ B(\cup):A 或 B 或兩者中的所有元素。intersect(A, B)或A ∩ B(\cap):A 和 B 共同的所有元素。setdiff(A, B)(沒有符號):在 A 中但不在 B 中的元素。symdiff(A, B)(沒有符號):只在 A 或只在 B 中的元素,兩者皆有則不算。issubset(A, B)或A ⊆ B(\subseteq)或B ⊇ A(\supseteq):若 A 的所有元素也都在 B 中,則為true。issetequal(A, B)(沒有符號):若 A 和 B 包含完全相同的元素,則為true。isdisjoint(A, B)(沒有符號):若 A 和 B 沒有任何共同元素,則為true(也就是交集為空)。s1 = Set(1:4)
s2 = Set(3:6)
julia> s1 ∪ s2 # union
Set{Int64} with 6 elements:
5
4
6
2
3
1
julia> s1 ∩ s2 # intersect
Set{Int64} with 2 elements:
4
3
julia> setdiff(s1, s2)
Set{Int64} with 2 elements:
2
1
julia> symdiff(s1, s2)
Set{Int64} with 4 elements:
5
6
2
1
julia> s1 ⊇ s2 # issubset
false
這些運算大多也有會修改原集合的版本,只要在函式名稱後面加上!。完整的函式清單請參閱手冊。