Die Aufgabe ist es eine simple Hashmap zu implementieren.
So sieht mein Entwurf dafür aus:
Allerdings spuckt die zugehörige JUnit Datei einige Fehler aus.
Die put(T key, V value) Methode scheint zu funktionieren, wenn ich nicht die Abfrage mache ob es das Objekt gibt(Also ohne die remove(T key) und ohne containsKey(T key)).
Die containsValue(V value) und die size() Methode hingegen funktionieren nicht wie vorgesehen.
Den Rest hab ich noch nicht getestet aber ich befürchte ähnliches.
Freue mich auf Hilfe
Mfg
So sieht mein Entwurf dafür aus:
Java:
package src;
import java.util.*;
public class SimpleHashMap<T, V> implements IHashMap<T, V> {
LinkedList<HashObject<T, V>>[] array;
Collection<V> collection;
public SimpleHashMap(int moduloX) {
array = new LinkedList[moduloX];
for (int i = 0; i < moduloX; i++) {
array[i] = new LinkedList<HashObject<T, V>>();
}
}
@Override
public void clear() {
/**
* Removes all mappings from this map.
*/
int m = array.length;
for (int i = 0; i < m; i++) {
array[i] = new LinkedList<HashObject<T, V>>();
}
}
@Override
public boolean containsKey(T key) {
/**
* Returns true if this map contains a mapping for the specified key.
* @param key
* @return true if key is contained. else false
*/
int h = key.hashCode();
h = h % array.length;
for (int i = 0; i <= array[h].size(); i++) {
if (array[h].get(i).equals(null)) {
break;
}
if (array[h].get(i).getKey().equals(key)) {
return true;
}
}
return false;
}
@Override
public boolean containsValue(V value) {
/**
* Returns true if this map maps one or more keys to the specified value.
* @param value
* @return
*/
for (int i = 0; i <= array.length; i++) {
if (array[i].contains(value)) {
return true;
}
}
return false;
}
@Override
public V get(T key) {
/**
* Returns the value to which the specified key is mapped
* in this identity hash map, or null if the map contains no mapping for this key.
*/
int h = key.hashCode();
h = h % array.length;
for (int i = 0; i <= array[h].size(); i++) {
if (array[h].get(i).equals(null)) {
break;
}
if (array[h].get(i).getKey().equals(key)) {
V value = array[h].get(i).getValue();
return value;
}
}
return null;
}
@Override
public boolean isEmpty() {
/**
* Returns true if this map contains no key-value mappings.
* @return
*/
boolean b = true;
int l = array.length;
for (int i = 0; i <= l; i++) {
if (array[i] != null) {
b = false;
}
}
return b;
}
@Override
public void put(T key, V value) {
/**
* Associates the specified value with the specified key in this map.
*/
int h = key.hashCode();
h = h % array.length;
if(containsKey(key)){
remove(key);
}
array[h].add(new HashObject<T, V>(key, value));
}
@Override
public void remove(T key) {
/**
* Removes the mapping for this key from this map if present.
*
* @param key
*/
int h = key.hashCode();
h = h % array.length;
for (int i = 0; i <= array[h].size(); i++) {
if (array[h].get(i).equals(null)) {
break;
}
if (array[h].get(i).getKey().equals(key)) {
array[h].remove(i);
}
}
}
@Override
public Collection<V> values() {
/**
* Returns a Collection containing all values in the HashMap
* @return
*/
for (int i = 0; i <= array.length; i++) {
for (int c = 0; c <= array[i].size(); c++) {
if (array[i].get(c).getValue().equals(null)) {
break;
}
else {
collection.add(array[i].get(c).getValue());
}
}
}
return collection;
}
@Override
public int size() {
/**
* Returns the number of key-value mappings in this map.
*
* @return
*/
int counter = 0;
for (int i = 0; i <= array.length; i++) {
if (array[i] != null) {
counter += array[i].size();
}
}
return counter;
}
}
Java:
package src;
public class HashObject<T, V> {
private T key;
private V value;
public HashObject(T key, V value) {
this.key = key;
this.value = value;
}
public T getKey(){
return key;
}
public V getValue(){
return value;
}
}
Allerdings spuckt die zugehörige JUnit Datei einige Fehler aus.
Java:
package src;
import java.util.Collection;
import junit.framework.Assert;
import org.junit.Test;
public class TestHashMap {
public static final int moduloX=1000;
@Test
public void testHash(){
SimpleHashMap<String, Integer> map = new SimpleHashMap<String, Integer>(moduloX);
String one = "1";
String two = "2";
String three = "3";
String four = "4";
map.put(one, 155);
Assert.assertEquals(new Integer(155), map.get(one));
map.put(one, 1555);
map.put(two, 13);
map.put(three, 663);
map.put(four, 15551);
Assert.assertEquals(new Integer(1555), map.get(one));
Assert.assertEquals(new Integer(13), map.get(two));
Assert.assertEquals(new Integer(663), map.get(three));
Assert.assertEquals(new Integer(15551), map.get(four));
Assert.assertEquals(true, map.containsValue(15551));
Collection<Integer> values = map.values();
Assert.assertTrue(values.contains(1555));
Assert.assertTrue(values.contains(13));
Assert.assertTrue(values.contains(663));
Assert.assertTrue(values.contains(15551));
Assert.assertEquals(4, values.size());
map.remove(four);
Assert.assertEquals(false, map.containsValue(15551));
Assert.assertEquals(null, map.get(four));
Assert.assertEquals(false, map.containsKey(four));
map.clear();
// Geschwindigkeitsmessung
long begin = System.currentTimeMillis();
for(int i = 0;i<100000;i++){
if(i%5000==0){
System.out.println(i+"");
}
map.put(new Integer(i).toString(),i);
Assert.assertEquals(new Integer(i),
map.get(new Integer(i).toString()));
}
long ende = System.currentTimeMillis();
System.out.println("Es wurden "+(ende-begin)/1000+" Sekunden benötigt");
}
}
Die put(T key, V value) Methode scheint zu funktionieren, wenn ich nicht die Abfrage mache ob es das Objekt gibt(Also ohne die remove(T key) und ohne containsKey(T key)).
Die containsValue(V value) und die size() Methode hingegen funktionieren nicht wie vorgesehen.
Den Rest hab ich noch nicht getestet aber ich befürchte ähnliches.
Freue mich auf Hilfe
Mfg