Demetrius Murray před 6 roky
rodič
revize
9805a534d4

+ 1
- 1
src/main/java/MyArrayList.java Zobrazit soubor

@@ -4,7 +4,7 @@ import java.util.Collection;
4 4
 
5 5
 public class MyArrayList<T> implements Cloneable{
6 6
 
7
-    private T[] inputArray;
7
+    protected T[] inputArray;
8 8
     private int capacity;
9 9
     private Integer size;
10 10
 

+ 133
- 0
src/main/java/MyMap.java Zobrazit soubor

@@ -0,0 +1,133 @@
1
+import java.util.*;
2
+
3
+public class MyMap<K,V>  {
4
+    MySet<Node<K,V>>[] bucket = new MySet[15];
5
+
6
+    public MyMap(){
7
+        for (int i=0; i<bucket.length; i++)
8
+            bucket[i] = new MySet<>();
9
+    }
10
+
11
+    public int size() {
12
+        int size = 0;
13
+        for (MySet m:bucket){
14
+            size += m.size();
15
+        }
16
+        return size;
17
+    }
18
+
19
+    public boolean isEmpty() {
20
+        for (MySet m:bucket){
21
+            if (!m.isEmpty())
22
+                return false;
23
+        }
24
+        return true;
25
+    }
26
+
27
+    public boolean containsKey(K key) {
28
+        for (Node<K,V> n : bucket[bucketIndex(key)])
29
+            if (n.getKey().equals(key))
30
+                return true;
31
+        return false;
32
+    }
33
+
34
+    public boolean containsValue(V value) {
35
+        for (int i = 0; i < 15; i++) {
36
+            for (Node<K, V> n : bucket[i])
37
+                if (n.getValue().equals(value))
38
+                    return true;
39
+        }
40
+        return false;
41
+    }
42
+
43
+    public V get(K key) {
44
+        for (Node<K,V> n : bucket[bucketIndex(key)])
45
+            if (n.getKey().equals(key))
46
+                return n.getValue();
47
+        return null;
48
+    }
49
+
50
+    public V put(K key, V value) {
51
+        for (Node<K,V> n : bucket[bucketIndex(key)]){
52
+            if (n.getKey().equals(key))
53
+                return n.setValue(value);
54
+            else if(n.hash == key.hashCode() && !n.getKey().equals(key))
55
+                n.next = new Node<>(key.hashCode(),key,value,null);
56
+        }
57
+
58
+        bucket[bucketIndex(key)].add(new Node<>(key.hashCode(), key, value, null));
59
+        return value;
60
+    }
61
+
62
+    public V remove(K key) {
63
+        for (Node<K,V> n : bucket[bucketIndex(key)])
64
+            if (n.getKey().equals(key)) {
65
+                bucket[bucketIndex(key)].remove(n);
66
+                return n.getValue();
67
+            }
68
+        return null;
69
+    }
70
+
71
+    public void putAll(Map<? extends K, ? extends V> m) {
72
+        m.forEach(this::put);
73
+    }
74
+
75
+    public void clear() {
76
+        for (MySet m : bucket){
77
+            m.clear();
78
+        }
79
+    }
80
+
81
+    public MySet<K> keySet() {
82
+        MySet<K> ks = new MySet<>();
83
+        for (MySet<Node<K,V>> m : bucket)
84
+            m.forEach(e -> ks.add(e.getKey()));
85
+        return ks;
86
+    }
87
+
88
+    public Collection<V> values() {
89
+        List<V> l = new ArrayList<>();
90
+        for (MySet<Node<K,V>> m : bucket)
91
+            m.forEach(e -> l.add(e.getValue()));
92
+        return l;
93
+    }
94
+
95
+    public MySet<Map.Entry<K, V>> entrySet() {
96
+        MySet<Map.Entry<K,V>> set = new MySet<>();
97
+        for (MySet<Node<K,V>> m : bucket)
98
+            m.forEach(set::add);
99
+        return set;
100
+    }
101
+
102
+    protected static class Node<K,V> implements Map.Entry<K, V> {
103
+        final K key;
104
+        V value;
105
+        Node<K,V> next;
106
+        final int hash;
107
+
108
+        Node(int hash, K key, V value, Node<K,V> next){
109
+            this.key = key;
110
+            this.value = value;
111
+            this.next = next;
112
+            this.hash = hash;
113
+        }
114
+
115
+        public K getKey() {
116
+            return key;
117
+        }
118
+
119
+        public V getValue() {
120
+            return value;
121
+        }
122
+
123
+        public V setValue(V value) {
124
+            V oldValue = this.value;
125
+            this.value = value;
126
+            return oldValue;
127
+        }
128
+    }
129
+
130
+    private int bucketIndex(K key){
131
+        return Math.abs(key.hashCode()) % bucket.length;
132
+    }
133
+}

+ 30
- 35
src/main/java/MySet.java Zobrazit soubor

@@ -1,8 +1,9 @@
1 1
 import java.util.Collection;
2
+import java.util.ConcurrentModificationException;
2 3
 import java.util.Iterator;
3
-import java.util.function.Consumer;
4
+import java.util.NoSuchElementException;
4 5
 
5
-public class MySet<T> implements Cloneable{
6
+public class MySet<T> implements Cloneable, Iterable<T>{
6 7
     MyArrayList<T> mySet;
7 8
 
8 9
     public MySet(){
@@ -69,45 +70,39 @@ public class MySet<T> implements Cloneable{
69 70
     }
70 71
 
71 72
     public Iterator<T> iterator(){
72
-
73
-        return null;
73
+        return new Iterator<T>() {
74
+            int cursor;
75
+            int lastRet = -1;
76
+
77
+            @Override
78
+            public boolean hasNext() {
79
+                return cursor != size();
80
+            }
81
+
82
+            @Override
83
+            public T next() {
84
+                int i = cursor;
85
+                if (i >= size())
86
+                    throw new NoSuchElementException();
87
+                T[] inputArray = mySet.inputArray;
88
+                if (i >= inputArray.length)
89
+                    throw new ConcurrentModificationException();
90
+                cursor = i + 1;
91
+                return inputArray[lastRet = i];
92
+            }
93
+        };
74 94
     }
75 95
 
76 96
     public boolean removeAll(Collection<T> c){
77
-
78
-        return false;
97
+        c.forEach(this::remove);
98
+        return true;
79 99
     }
80 100
 
81 101
     public boolean retainAll(Collection<T> c){
82
-
83
-        return false;
84
-    }
85
-
86
-    private class Itr implements Iterator<T> {
87
-        int cursor;
88
-        int lastRet = -1;
89
-
90
-        Itr(){}
91
-
92
-        @Override
93
-        public boolean hasNext() {
94
-            return cursor!= size();
95
-        }
96
-
97
-        @Override
98
-        public T next() {
99
-            return null;
100
-        }
101
-
102
-        @Override
103
-        public void remove() {
104
-
105
-        }
106
-
107
-        @Override
108
-        public void forEachRemaining(Consumer<? super T> action) {
109
-
110
-        }
102
+        for (T t:mySet.inputArray)
103
+            if (!c.contains(t))
104
+                mySet.remove(t);
105
+        return true;
111 106
     }
112 107
 
113 108
 }

+ 89
- 0
src/test/java/MyMapTest.java Zobrazit soubor

@@ -0,0 +1,89 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+import java.util.HashMap;
5
+import java.util.Map;
6
+
7
+public class MyMapTest {
8
+    MyMap<String, Integer> myMap = new MyMap<>();
9
+
10
+    @Test
11
+    public void testPut(){
12
+        myMap.put("One", 1);
13
+        Assert.assertNotNull(myMap);
14
+    }
15
+    @Test
16
+    public void testSize(){
17
+        myMap.put("One", 1);
18
+        int actual = myMap.size();
19
+        Assert.assertEquals(1, actual);
20
+    }
21
+
22
+    @Test
23
+    public void testIsEmpty(){
24
+        Assert.assertTrue(myMap.isEmpty());
25
+    }
26
+
27
+    @Test
28
+    public void testContainsKey(){
29
+        myMap.put("One",1);
30
+        Assert.assertTrue(myMap.containsKey("One"));
31
+    }
32
+
33
+    @Test
34
+    public void testContainsValue(){
35
+        myMap.put("One",1);
36
+        Assert.assertTrue(myMap.containsValue(1));
37
+    }
38
+
39
+    @Test
40
+    public void testGet(){
41
+        myMap.put("One",1);
42
+        Integer expected = 1;
43
+        Assert.assertEquals(expected, myMap.get("One"));
44
+    }
45
+
46
+    @Test
47
+    public void testRemove(){
48
+        myMap.put("One",1);
49
+        myMap.remove("One");
50
+        Assert.assertEquals(0, myMap.size());
51
+    }
52
+
53
+    @Test
54
+    public void testPutAll(){
55
+        Map<String, Integer> map = new HashMap<>();
56
+        map.put("Two", 2);
57
+        map.put("Three", 3);
58
+        myMap.putAll(map);
59
+        Assert.assertEquals(2, myMap.size());
60
+    }
61
+
62
+    @Test
63
+    public void testClear(){
64
+        myMap.put("One", 1);
65
+        myMap.clear();
66
+        Assert.assertEquals(0, myMap.size());
67
+    }
68
+
69
+    @Test
70
+    public void testKeySet(){
71
+        myMap.put("One", 1);
72
+        myMap.put("Two", 2);
73
+        Assert.assertEquals("Two", myMap.keySet().get(1));
74
+    }
75
+
76
+    @Test
77
+    public void testValues(){
78
+        myMap.put("One", 1);
79
+        myMap.put("Two", 2);
80
+        Assert.assertEquals(2, myMap.values().size());
81
+    }
82
+
83
+    @Test
84
+    public void testEntrySet(){
85
+        myMap.put("One", 1);
86
+        myMap.put("Two", 2);
87
+        Assert.assertEquals("Two", myMap.entrySet().get(1).getKey());
88
+    }
89
+}

+ 36
- 2
src/test/java/MySetTest.java Zobrazit soubor

@@ -141,12 +141,12 @@ public class MySetTest {
141 141
     }
142 142
 
143 143
     @Test
144
-    public void iterator() {
144
+    public void testIterator() {
145 145
         Assert.assertNotNull(ms.iterator());
146 146
     }
147 147
 
148 148
     @Test
149
-    public void iteratorHasNext() {
149
+    public void testIteratorHasNext() {
150 150
         ms.add(1);
151 151
         ms.add(3);
152 152
         ms.add(2);
@@ -158,4 +158,38 @@ public class MySetTest {
158 158
         Assert.assertEquals(three,it.next());
159 159
         Assert.assertEquals(two,it.next());
160 160
     }
161
+
162
+    @Test
163
+    public void testRemoveAll(){
164
+        ms.add(1);
165
+        ms.add(3);
166
+        ms.add(2);
167
+
168
+        List<Integer> l = new ArrayList<>();
169
+        l.add(3);
170
+        l.add(2);
171
+        l.add(5);
172
+
173
+        ms.removeAll(l);
174
+
175
+        Integer expected = 1;
176
+        Assert.assertEquals(expected,ms.size());
177
+    }
178
+
179
+    @Test
180
+    public void testRetainAll(){
181
+        ms.add(1);
182
+        ms.add(3);
183
+        ms.add(2);
184
+        ms.add(4);
185
+
186
+        List<Integer> l = new ArrayList<>();
187
+        l.add(3);
188
+        l.add(2);
189
+        l.add(5);
190
+
191
+        ms.retainAll(l);
192
+        Integer expected = 2;
193
+        Assert.assertEquals(expected, ms.size());
194
+    }
161 195
 }