Hey,
ich habe folgende Aufgabe:
Ich soll eine HashMap schreiben, die mit den beiden generischen Parametern K und V funktioniert. Dabei soll die BucketHashing Methode verwendet werden.
Die Hauptklasse ist folgende:
die HashEntry Klasse dazu sieht wie folgt aus:
Wie ihr sicherlich seht, habe ich die remove und die clear Methoden noch nicht implementiert. Als Hinweis: ich kann die Methoden nicht ändern, also remove z.B. static machen. Wie kann ich Mein Ansatz wäre hierbei an den Punkt für den entsprechenden Key zu gehen und hier den Value einfach mit .remove zu entfernen. Hierbei bekomme ich aber die Meldung, dass ich meine Methode auf static umstellen muss.
Mein Ansatz für die clear Methode wäre einfach, das bucketArray auf [0] zu setzen, sodass ich "keine Eimer" mehr habe. Ist der Gedanke grundsätzlich richtig und wie kann ich values zu bestimmten Elementen sonst noch entfernen?
LG
ich habe folgende Aufgabe:
Ich soll eine HashMap schreiben, die mit den beiden generischen Parametern K und V funktioniert. Dabei soll die BucketHashing Methode verwendet werden.
Die Hauptklasse ist folgende:
Java:
package programming.set11.brownies;
public class BrownieMap<K, V> {
HashEntry[] bucketArray;
/**
* The capacity sets the length of the bucket array.
*
* @param numberOfBuckets
* bucket array length
* @throws IllegalArgumentException
* if the number of buckets is smaller than one.
*/
public BrownieMap(int numberOfBuckets) {
if (numberOfBuckets < 1) {
throw new IllegalArgumentException("Plese enter a number > 1.");
}
HashEntry[] bucketArray = new HashEntry[numberOfBuckets];
this.bucketArray = bucketArray;
}
/**
* Put an item into the map.
*
* @param key
* the key of the item.
* @param value
* the value of the item.
* @throws IllegalArgumentException
* if the key or the value is {@code null}.
*/
public void put(K key, V value) {
if (key == null || value == null) {
throw new IllegalArgumentException("Plese enter a number > 1.");
}
int bucket = Math.abs(key.hashCode()) % bucketArray.length;
HashEntry entry = findEntry(bucketArray[bucket], key);
if (entry == null) {
entry = new HashEntry(key, value);
entry.setLink(bucketArray[bucket]);
bucketArray[bucket] = entry;
} else {
entry.setValue(value);
}
}
/**
* Returns the value of a given key.
*
* @param key
* the key of the item.
* @return the value belonging to that key or {@code null} if no value is mapped
* to that key.
* @throws IllegalArgumentException
* if the key is {@code null}.
*/
public V get(K key) {
if (key == null) {
throw new IllegalArgumentException("Plese enter a number > 1.");
}
int bucket = Math.abs(key.hashCode()) % bucketArray.length;
HashEntry entry = findEntry(bucketArray[bucket], key);
if (entry == null) {
return null;
} else {
return (V) entry.getValue();
}
}
/**
* Removes one key and its value from the map. If the key does not exist in the
* map, nothing happens.
*
* @param key
* the key.
* @throws IllegalArgumentException
* if the key is {@code null}.
*/
public void remove(K key) {
if(key == null) {
throw new IllegalArgumentException("key can not be null.");
}
int bucket = Math.abs(key.hashCode()) % bucketArray.length;
HashEntry entry = findEntry(bucketArray[bucket], key);
if(entry != null) {
HashEntry.remove(entry);
} else {
}
}
/**
* Clears the entire map.
*/
public void clear() {
// Implement me!
}
/*
* Scans the entry chain looking for an entry that matches the specified key. If
* no such entry exists, findEntry returns null.
*/
private HashEntry findEntry(HashEntry entry, K key) {
while (entry != null) {
if (entry.getKey().equals(key))
return entry;
entry = entry.getLink();
}
return null;
}
}
die HashEntry Klasse dazu sieht wie folgt aus:
Java:
package programming.set11.brownies;
/* Package-private class: HashEntry */
/*
* This class represents a pair of a key and a value, along with a reference
* to the next HashEntry in the chain. The methods exported by the class
* consist only of getters and setters.
*/
public class HashEntry<K,V> {
/* Creates a new HashEntry for the specified key/value pair */
public HashEntry(K key, V value) {
entryKey = key;
entryValue = value;
}
/* Returns the key component of a HashEntry */
public K getKey() {
return entryKey;
}
/* Returns the value component of a HashEntry */
public V getValue() {
return entryValue;
}
/* Sets the value component of a HashEntry to a new value */
public void setValue(V value) {
entryValue = value;
}
/* Returns the next link in the entry chain */
public HashEntry getLink() {
return entryLink;
}
/* Sets the link to the next entry in the chain */
public void setLink(HashEntry nextEntry) {
entryLink = nextEntry;
}
/* Private instance variables */
private K entryKey; /* The key component for this HashEntry */
private V entryValue; /* The value component for this HashEntry */
private HashEntry entryLink; /* The next entry in the chain */
}
Wie ihr sicherlich seht, habe ich die remove und die clear Methoden noch nicht implementiert. Als Hinweis: ich kann die Methoden nicht ändern, also remove z.B. static machen. Wie kann ich Mein Ansatz wäre hierbei an den Punkt für den entsprechenden Key zu gehen und hier den Value einfach mit .remove zu entfernen. Hierbei bekomme ich aber die Meldung, dass ich meine Methode auf static umstellen muss.
Mein Ansatz für die clear Methode wäre einfach, das bucketArray auf [0] zu setzen, sodass ich "keine Eimer" mehr habe. Ist der Gedanke grundsätzlich richtig und wie kann ich values zu bestimmten Elementen sonst noch entfernen?
LG