Skip to content

Commit 07e2a2a

Browse files
committed
added iterator in arraylist and added entrySet method in HashMap
1 parent 40d328f commit 07e2a2a

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

src/Lists/ArrayListCustom.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.*;
44
import java.util.function.Consumer;
5-
import java.util.function.Function;
65
import java.util.function.Predicate;
76

87
public class ArrayListCustom<T> implements Cloneable {
@@ -123,6 +122,7 @@ public void forEach(Consumer<T> o) {
123122
}
124123

125124
public ArrayListCustom<T> filter(Predicate<T> o) {
125+
Objects.requireNonNull((o));
126126
ArrayListCustom<T> newList = new ArrayListCustom<>();
127127
this.forEach((item) -> {
128128
if (o.test(item)) {
@@ -132,6 +132,21 @@ public ArrayListCustom<T> filter(Predicate<T> o) {
132132
return newList;
133133
}
134134

135+
//hh
136+
private class ArrayListItr<T> implements Iterator<T> {
137+
int cursor = 0;
138+
139+
@Override
140+
public boolean hasNext() {
141+
return cursor < ArrayListCustom.this.size;
142+
}
143+
144+
@Override
145+
public T next() {
146+
return (T) ArrayListCustom.this.arr[cursor++];
147+
}
148+
}
149+
135150
public Object[] toArray() {
136151
Object[] ret = new Object[size];
137152
for (int i = 0; i < size; i++) {

src/Maps/HashMapCustom.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,36 @@
22
//check out my medium blog for hashmap
33
//https://medium.com/@nisabmohd/hashmap-in-java-c010dec8fbd0
44

5-
import java.util.Objects;
5+
import java.util.*;
66
import java.util.function.BiConsumer;
7+
import java.util.function.Consumer;
78

89
public class HashMapCustom<K, V> {
910

1011
private final int DEFAULT_CAPACITY = 17;
1112
private final Entry[] bucket = new Entry[DEFAULT_CAPACITY];
1213
private int size = 0;
1314

14-
private class Entry<K, V> {
15+
public class Entry<K, V> {
1516

16-
K key;
17-
V val;
18-
Entry next;
17+
private K key;
18+
private V val;
19+
private Entry next;
1920

2021
public Entry(K key, V val, Entry next) {
2122
this.key = key;
2223
this.val = val;
2324
this.next = next;
2425
}
2526

27+
public K getKey() {
28+
return this.key;
29+
}
30+
31+
public V getValue() {
32+
return this.val;
33+
}
34+
2635
}
2736

2837
private int hashCode(K key) {
@@ -135,6 +144,17 @@ public void forEach(BiConsumer<K, V> o) {
135144
}
136145
}
137146

147+
public void forEachEntry(Consumer<Entry<K, V>> o) {
148+
Objects.requireNonNull(o);
149+
for (int i = 0; i < bucket.length; i++) {
150+
Entry temp = bucket[i];
151+
while (temp != null) {
152+
o.accept(temp);
153+
temp = temp.next;
154+
}
155+
}
156+
}
157+
138158
@Override
139159
public String toString() {
140160
StringBuilder builder = new StringBuilder("{");
@@ -167,4 +187,12 @@ public Object[] toKeyArray() {
167187
return t;
168188
}
169189

190+
public Set<Entry<K, V>> entrySet() {
191+
Set<Entry<K, V>> set = new HashSet<>();
192+
forEachEntry(entry -> {
193+
set.add(entry);
194+
});
195+
return set;
196+
}
197+
170198
}

0 commit comments

Comments
 (0)