首先,我有一个扩展AbstractMap的类。

public class ShowMap <K, V> extends AbstractMap <K, V>


类具有两个List类型的容器:

private List <K> keys = new ArrayList <K> ();
private List <V> values = new ArrayList <V> ();


类也有几种方法。
方法V接受键之类的参数,并将其放入列表键中:

public V put (K key, V value){
    V oldValue = this.get(key);
    if (!keys.contains(key)){
        keys.add(key);
        values.add(value);
    }
    else{
        values.set(keys.indexOf(key), value);
    }

    return oldValue;
}


方法V get从List值返回一个值,否则方法teturn null:

public V get (Object obje){
    if (!keys.contains(obje))
        return null;
    return values.get(keys.indexOf(obje));
}


以下方法构成了一组MapEntry,但是它不起作用:

public Set<java.util.Map.Entry<K, V>> entrySet() {

    Set <Map.Entry<K, V>> sets = new HashSet <Map.Entry<K, V>> ();
    Iterator<K> ki = keys.iterator();
     Iterator<V> vi = values.iterator();

     while (ki.hasNext()){
        sets.add(new MapEntry <K, V> (ki.next(), vi.next())); // This line should to work, however It doesn't work...
     }
    return sets;
}


主要方法当然是:

public static void main (String [] args){
    ShowMap <String, String> SM = new ShowMap <String, String> ();
    SM.putAll(Countries.FMap.capitals(10));
    System.out.println(SM);
    System.out.println(SM.get("Moscow"));
    System.out.println(SM.entrySet());
}


非常感谢:)

最佳答案

public Set<java.util.Map.Entry<K, V>> entrySet() {
        Set <Map.Entry<K, V>> sets = new HashSet <Map.Entry<K, V>> ();
        Iterator<K> ki = keys.iterator();
         Iterator<V> vi = values.iterator();

         while (ki.hasNext()){
            sets.add(new MapEntry <K, V> (ki.next(), vi.next())); // This line should to work, however It doesn't work...
         }
        return sets;
    }


在这里,您将返回一组Map.Entry而不是MapEntry。而且,MapEntry不是类。请更新代码。

10-04 19:30