Hi danke für deine Antwort.
ich habe Entry so implmentiert. Da ich setValue() brauche.
public class SimpleEntry<K, V> implements Entry<K, V> {
private K key;
private V value;
public SimpleEntry(K k, V v) {
key = k;
value = v;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
protected void setKey(K key) {
this.key = key;
}
protected void setValue(V value) {
this.value = value;
}
}
hier ist meine Mapclass :
/* @author XXX
* @created on 16.05.2008 22:27:16
*/
package uebungen.aufgabe26;
import java.util.*;
import uebungen.aufgabe26.SimpleEntry;;
public class MyMap<K, V> implements Map<K, V> {
// create a list type of SimpleEntry<Key,Value>
LinkedList<SimpleEntry<K,V>> myllist = new LinkedList<SimpleEntry<K, V>>();
@Override
public Iterable<Entry<K, V>> entries() {
// TODO Auto-generated method stub
return null;
}
@Override
//The method get delivers the value of the given key
// Runtime O(n)
public V get(K key) {
// TODO Auto-generated method stub
Iterator<SimpleEntry<K, V>> getIt = myllist.iterator();
while(getIt.hasNext()) {
SimpleEntry<K, V> currGetEntry = getIt.next();
if (currGetEntry.getKey()==key)
return currGetEntry.getValue();
}
return null;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return (myllist.size()==0);
}
@Override
/* The method put delivers the value of the key which already exists otherwise a new key 'll be added
* with given key and value */
// Runtime O(n)
public V put(K key, V value) {
// TODO Auto-generated method stub
Iterator<SimpleEntry<K, V>> putIt = myllist.iterator();
V oldValue = null ;
SimpleEntry<K,V> toInsert = new SimpleEntry<K,V>(key, value); //create a new entry with key and value
while(putIt.hasNext()) {
SimpleEntry<K, V> currPutEntry = putIt.next();
if (currPutEntry.getKey()==key) {
oldValue = currPutEntry.getValue() ;
currPutEntry.setValue(value) ;
return oldValue; // return the old key
}
}
myllist.addLast(toInsert); // add a new entry if the key doesn't exist
return null;
}
@Override
public V remove(K key) {
// TODO Auto-generated method stub
return null;
}
@Override
public int size() {
// TODO Auto-generated method stub
return myllist.size();
}
}
wie kann ich hier die Methode entries() implementieren ?