committed by
chris
3 changed files with 0 additions and 153 deletions
@ -1,107 +0,0 @@ |
|||
package solution; |
|||
|
|||
import java.util.function.Consumer; |
|||
|
|||
import testat.ISimpleMap; |
|||
|
|||
public class SimpleArrayMap<K, V> implements ISimpleMap<K, V> { |
|||
|
|||
private int size; |
|||
private K[] keys; |
|||
private V[] values; |
|||
|
|||
public SimpleArrayMap(int maxSize) { |
|||
size = 0; |
|||
keys = (K[]) new Object[maxSize]; |
|||
values = (V[]) new Object[maxSize]; |
|||
} |
|||
|
|||
@Override |
|||
public V put(K k, V v) { |
|||
|
|||
if (k == null || v == null) { |
|||
return null; |
|||
} |
|||
for (int i = 0; i < values.length; i++) { |
|||
if (k.equals(keys[i])) { // found
|
|||
V result = values[i]; |
|||
values[i] = v; |
|||
return result; |
|||
} |
|||
} |
|||
for (int i = 0; i < values.length; i++) { |
|||
if (keys[i] == null) { // empty
|
|||
keys[i] = k; |
|||
values[i] = v; |
|||
size++; |
|||
return null; |
|||
} |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public V get(K k) { |
|||
if (k == null) { |
|||
return null; |
|||
} |
|||
for (int i = 0; i < values.length; i++) { |
|||
if (k.equals(keys[i])) { // found
|
|||
return values[i]; |
|||
} |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public V remove(K k) { |
|||
if (k == null) { |
|||
return null; |
|||
} |
|||
for (int i = 0; i < values.length; i++) { |
|||
if (k.equals(keys[i])) { // found
|
|||
V v = values[i]; |
|||
size--; |
|||
keys[i] = null; |
|||
values[i] = null; |
|||
return v; |
|||
} |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public boolean contains(V v) { |
|||
if (v == null) { |
|||
return false; |
|||
} |
|||
int counter = 0; |
|||
for (int i = 0; i < values.length; i++) { |
|||
if (v.equals(values[i])) { |
|||
return true; |
|||
} |
|||
if (values[i] != null) { |
|||
counter++; |
|||
} |
|||
if (counter == size) { |
|||
return false; |
|||
} |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
@Override |
|||
public void forEach(Consumer<? super V> consumer) { |
|||
for (int i = 0; i < values.length; i++) { |
|||
if (keys[i] != null) { // found
|
|||
consumer.accept(values[i]); |
|||
} |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public int size() { |
|||
return size; |
|||
} |
|||
|
|||
} |
@ -1,13 +0,0 @@ |
|||
package solution; |
|||
|
|||
import testat.ASimpleMapTest; |
|||
import testat.ISimpleMap; |
|||
|
|||
public class SimpleArrayMapTest extends ASimpleMapTest { |
|||
|
|||
@Override |
|||
protected ISimpleMap<String, String> getInstance() { |
|||
return new SimpleArrayMap<>(5); |
|||
} |
|||
|
|||
} |
@ -1,33 +0,0 @@ |
|||
package testat; |
|||
|
|||
import java.util.function.Consumer; |
|||
|
|||
public interface ISimpleMap<K, V> { |
|||
|
|||
// speichert zu den Schlüssel k den Wert v, sofern beide nicht null sind (in
|
|||
// diesem Fall wird null zurück gegeben) .
|
|||
// Falls zu k bereits ein Wert w vorhanden ist, wird dieser durch v ersetzt. Der
|
|||
// alte
|
|||
// Wert w wird zurückgegebn. Falls zu k noch kein Wert gespeichert war, wird
|
|||
// null zurückgegeben
|
|||
V put(K k, V v); |
|||
|
|||
// Liefert den zum Schlüssel k gespeicherten Wert. Liefert null, falls k ==
|
|||
// null oder falls zu k kein Wert gespeichert war
|
|||
V get(K k); |
|||
|
|||
// Entfernt den zum Schlüssel k gespeicherten Wert v und liefert v zurück.
|
|||
// Liefert null, falls k ==
|
|||
// null oder falls zu k kein Wert gespeichert war
|
|||
V remove(K k); |
|||
|
|||
// Liefert true, falls der Wert v in der Map gespeichert ist
|
|||
boolean contains(V v); |
|||
|
|||
// wendet die Methode consumer.accept(v) auf alle in der Map gespeicherten Werte
|
|||
// an
|
|||
void forEach(Consumer<? super V> consumer); |
|||
|
|||
int size(); // Anzahl an in der Map gespeicherten Werte
|
|||
|
|||
} |
Loading…
Reference in new issue