-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashSetNew.java
199 lines (147 loc) · 4.54 KB
/
HashSetNew.java
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* HashSetNew.java
*
* Version:
* $1.1$
*/
import java.util.*;
/**
* HashSetNew is our implementation of the inbuilt Hashset. We override the add(),contains(),isEmpty(),size(),remove(),clear() methods.
*
* @author Shweta Yakkali
* @author Aishwarya Desai
*/
public class HashSetNew extends HashSet {
static Object hashsetarray[]=new Object[20050];
static int indexno=0;
static int count=0;
/**
* The add() is the overridden method where elements are added into the hashsetarray [].
* @param item Object
* @return boolean
*/
public boolean add(Object item){
boolean flag=true;
if(isEmpty()){
hashsetarray[indexno]=item;
indexno++;
flag=true;
}
else{
here:for(int i=0;i<indexno;i++){
if(hashsetarray[i]==item){
flag=false;
break here;
}
}
if(flag==true){
hashsetarray[indexno]=item;
indexno++;
flag=true;
}
}
return flag;
}
/**
* The isEmpty() is the overridden method where we check if the hashsetarray is empty or not.
* @return boolean
*/
public boolean isEmpty(){
if(indexno==0)
return true;
return false;
}
/**
* The size() is the overridden method where current size of the array is checked.
* @return int
*/
public int size(){
return indexno;
}
/**
* The clear() is the overridden method where we clear the hashsetarray and reset the index.
* @return int
*/
public void clear(){
for(int i=0;i<indexno;i++)
hashsetarray[i]=null;
indexno=0;
}
/**
* The contains() is the overridden method where we check if the given item is present in the hashsetarray or not.
* @param item Object
* @return boolean
*/
public boolean contains(Object item){
for(int i=0;i<size();i++){
if(hashsetarray[i]==item){
return true;
}
}
return false;
}
/**
* The remove() is the overridden method where we remove the particular object and decrement the size by 1.
* @param item Object
* @return boolean
*/
public boolean remove(Object item){
int index=0;
for( int i=0;i<size();i++){
if(hashsetarray[i]==item){
index=i;
break;
}
}
for(int i=index;i<size()-1;i++){
hashsetarray[i]=hashsetarray[i+1];
}
indexno=indexno-1;
return true;
}
/**
* The iterator() is the overridden method where we iterate over the hashsetarray.
* @return Iterator
*/
public Iterator iterator(){
return new MyIterator();
}
/**
* MyIterator is our implementation of inbuilt Iterator. We override the remove(),hasNext(),next() methods.
*
* @author Shweta Yakkali
* @author Aishwarya Desai
*/
class MyIterator implements Iterator{
int itervar=0;
/**
* The remove() is the overridden method.
*
*/
public void remove()
{}
/**
* The hasNext() is the overridden method wherein we check if the list is still traversable.
* @return boolean
*/
public boolean hasNext(){
boolean flag=false;
if(itervar<hashsetarray.length){
flag=true;
}
else{
flag=false;
}
return flag;
}
/**
* The next() is the overridden method wherein we return the next item by incrementing the counter.
* @return boolean
*/
public Object next(){
int i=itervar;
itervar++;
return hashsetarray[i];
}
}
}