-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyHashSet.java
More file actions
181 lines (151 loc) · 4.56 KB
/
Copy pathMyHashSet.java
File metadata and controls
181 lines (151 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package HashSet;
public class MyHashSet<E> {
public static class Node<E>{
final int hash;
final E element;
Node<E> next;
Node(int hash, E element, Node<E> next){
this.hash = hash;
this.element = element;
this.next = next;
}
}
private static final int DEFAULT_CAPACITY = 16;
private static final float LOAD_FACTOR = 0.75f;
private Node<E>[] table;
private int size;
@SuppressWarnings("unchecked")
public MyHashSet(){
this.table = new Node[DEFAULT_CAPACITY];
this.size = 0;
}
private int hash(E element){
int h = element.hashCode();
h ^=(h>>>16);
return h & (table.length - 1);
}
public boolean add(E element){
if(element == null)
throw new NullPointerException("NPE");
int index = hash(element);
Node<E> current = table[index];
while(current != null){
if(current.element.equals(element)){
return false; // 이미 데이터가 있다
}
current = current.next;
}
table[index] = new Node<>(index, element, table[index]);
this.size ++;
if(size > table.length*LOAD_FACTOR)
resize();
return true;
}
public boolean remove(E element){
if(element == null)
throw new NullPointerException("NPE");
int index = hash(element);
Node<E> current = table[index];
Node<E> prev = null;
while(current != null){
if(current.element.equals(element)){
if(prev == null){
table[index] = current.next;
}
else{
prev.next = current.next;
}
this.size --;
return true;
}
prev = current;
current = current.next;
}
return false;
}
public boolean contains(E element){
if(element == null)
throw new NullPointerException("NPE");
int index = hash(element);
Node<E> current = table[index];
while(current != null){
if(current.element.equals(element))
return true;
current = current.next;
}
return false;
}
public int size(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
@SuppressWarnings("unchecked")
private void resize(){
Node<E> oldTable[] = table;
table = new Node[oldTable.length*2];
size = 0;
for(int i = 0; i<oldTable.length; i++){
Node<E> current = oldTable[i];
while(current != null){
add(current.element);
current = current.next;
}
}
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder("[");
int check = 0;
for(int i = 0; i<table.length; i++){
Node<E> current = table[i];
while(current != null){
sb.append(current.element);
if(check < size - 1){
sb.append(", ");
}
check ++;
current = current.next;
}
}
sb.append("]");
return sb.toString();
}
// public void put(E element){
// if(element == null)
// throw new NullPointerException("NPE");
// int index = hash(element);
// Node<E> current = table[index];
// while(current != null){
// if(current.element.equals(element))
// return;
// current = current.next;
// }
// table[index] = new Node<>(index, element, table[index]);
// this.size ++;
// if(this.size > table.length * LOAD_FACTOR)
// resize();
// }
// public void remove(E element){
// if(element == null)
// throw new NullPointerException("NPE");
// int index = hash(element);
// Node<E> current = table[index];
// Node<E> prev = null;
// while(current != null){
// if(current.element.equals(element)){
// if(prev == null){
// table[index] = current.next;
// }
// else{
// prev.next = current.next;
// }
// this.size --;
// return;
// }
// prev = current;
// current = current.next;
// }
// }
}