Hi, I have an example class SlowMap taken from a book, unfortunatelly it doesn't compile and throws this exception:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at chapter17.SlowMap.entrySet(SlowMap.java:47)
at java.util.AbstractMap.toString(AbstractMap.java:478)
at java.lang.String.valueOf(String.java:2826)
at java.io.PrintStream.println(PrintStream.java:771)
at chapter17.SlowMap.main(SlowMap.java:54)
Java Result: 1
This is the code:
import java.util.*;
public class SlowMap<K, V> extends AbstractMap<K, V> {
private List<K> keys = new ArrayList<K>();
private List<V> values = new ArrayList<V>();
@Override
public V put(K key, V value) {
V oldValue = get(key);
if (!keys.contains(key)) {
keys.add(key);
values.add(value);
} else
values.set(keys.indexOf(key), value);
return oldValue;
}
@Override
public V get(Object key) {
if (!keys.contains(key))
return null;
return values.get(keys.indexOf(key));
}
@Override
public Set<Map.Entry<K, V>> entrySet() {
Set<Map.Entry<K, V>> set = new HashSet<Map.Entry<K, V>>();
Iterator<K> ki = keys.iterator();
Iterator<V> vi = values.iterator();
while (ki.hasNext())
// this line throws exception
set.add(new Map.Entry<K, V>(ki.next(), vi.next()));
return set;
}
public static void main(String[] args) {
SlowMap<String, String> m = new SlowMap<String, String>();
m.putAll(Countries.capitals(6));
System.out.println(m);
System.out.println(m.get("BUĊGARIA"));
System.out.println(m.entrySet());
}
}
I don't understand the exception message, so I have no clue how to fix the code. Can anyone tell me what is wrong with this code?
Thanks,
PR.