1 | # 哈希集合 HashSet\<T>
|
2 | 哈希集合实现了Set接口,它不允许集合中出现重复元素,底层实现为哈希表(HashTable),但是已集合元素为键,已布尔类型为值。具体详见哈希表(HashTable)
|
3 |
|
4 | ## 基本操作的API及示例
|
5 |
|
6 | ###通过数组构造集合 fromArray
|
7 | ##### [s] HashSet fromArray(T[] array)
|
8 | ``` text
|
9 | 实例:
|
10 | const array = Array.from({length:10}).map((item, index) => index);
|
11 | const hashSet = HashSet.fromArray(array);
|
12 |
|
13 | ```
|
14 |
|
15 | ### Constructor
|
16 | ##### new HashSet(number capacity = HashSet.DEFAULT_TABLE_SIZE)
|
17 | ``` text
|
18 | 实例:
|
19 | const hashSet = new HashSet();
|
20 |
|
21 | const array = Array(50).fill(0);
|
22 | const hashSet2 = new HashSet(array.length);
|
23 | ```
|
24 |
|
25 | ### 插入 add
|
26 | ##### HashSet add(T value)
|
27 | ``` text
|
28 | 实例:
|
29 | const hashSet = new HashSet();
|
30 | hashSet.add("1");
|
31 | 描述:
|
32 | 对象类型通过JSON序列化为字符串
|
33 | ```
|
34 | ### 删除 remove
|
35 | ##### HashSet remove(T value)
|
36 | ``` text
|
37 | 实例:
|
38 | const hashSet = new HashSet();
|
39 | hashSet.add("1");
|
40 |
|
41 | hashSet.remove("1");
|
42 | ```
|
43 |
|
44 | ### 是否存在 has
|
45 | ##### boolean has(T value)
|
46 | ``` text
|
47 | 实例:
|
48 | const hashSet = new HashSet();
|
49 | hashSet.add("1");
|
50 |
|
51 | hashSet.has("1");
|
52 | ```
|
53 |
|
54 | ### 清空 clear
|
55 | ##### void clear()
|
56 | ``` text
|
57 | 实例:
|
58 | const hashSet = new HashSet();
|
59 | hashSet.add("1");
|
60 |
|
61 | hashSet.clear();
|
62 | console.log(hashSet.Size);
|
63 | // 0
|
64 | ```
|
65 |
|
66 | ### 获取所有值 entries
|
67 | ##### T[] entries()
|
68 | ``` text
|
69 | 实例:
|
70 | const hashSet = new HashSet();
|
71 | hashSet.add(1);
|
72 | hashSet.add(2);
|
73 | hashSet.add(3);
|
74 | hashSet.add(4);
|
75 |
|
76 | const values = hashSet.entries();
|
77 | console.log(values);
|
78 | // [1, 2, 3, 4]
|
79 | ```
|
80 |
|
81 | ### 差集 diff
|
82 | ##### T[] diff(AbstractSet<T> set)
|
83 | ``` text
|
84 | 实例:
|
85 | const hashSet = new HashSet();
|
86 | hashSet.add(1);
|
87 | hashSet.add(2);
|
88 | hashSet.add(3);
|
89 | hashSet.add(4);
|
90 |
|
91 | const hashSet2 = new HashSet();
|
92 | hashSet2.add(2);
|
93 | hashSet2.add(3);
|
94 | hashSet2.add(4);
|
95 | hashSet2.add(5);
|
96 |
|
97 | const values = hashSet.diff(hashSet2);
|
98 | console.log(values);
|
99 | // [1]
|
100 | ```
|
101 |
|
102 | ### 并集 union
|
103 | ##### T[] union(AbstractSet<T> set)
|
104 | ``` text
|
105 | 实例:
|
106 | const hashSet = new HashSet();
|
107 | hashSet.add(1);
|
108 | hashSet.add(2);
|
109 | hashSet.add(3);
|
110 | hashSet.add(4);
|
111 |
|
112 | const hashSet2 = new HashSet();
|
113 | hashSet2.add(2);
|
114 | hashSet2.add(3);
|
115 | hashSet2.add(4);
|
116 | hashSet2.add(5);
|
117 |
|
118 | const values = hashSet.union(hashSet2);
|
119 | console.log(values);
|
120 | // [1, 2, 3, 4, 5]
|
121 | ```
|
122 |
|
123 | ### 交集 intersect
|
124 | ##### T[] intersect(AbstractSet<T> set)
|
125 | ``` text
|
126 | 实例:
|
127 | const hashSet = new HashSet();
|
128 | hashSet.add(1);
|
129 | hashSet.add(2);
|
130 | hashSet.add(3);
|
131 | hashSet.add(4);
|
132 |
|
133 | const hashSet2 = new HashSet();
|
134 | hashSet2.add(2);
|
135 | hashSet2.add(3);
|
136 | hashSet2.add(4);
|
137 | hashSet2.add(5);
|
138 |
|
139 | const values = hashSet.intersect(hashSet2);
|
140 | console.log(values);
|
141 | // [2, 3, 4]
|
142 | ``` |
\ | No newline at end of file |