Buenas a todos, hace días empecé a implementar la 2da parte del TreeMap casero implementando SortedMap, he estado teniendo problemas con la función tailMap, en concreto con el iterator de la clase interna para dicho fin el error es éste:
Cita: incompatible types: Entry<K#1,V#1> cannot be converted to Entry<K#2,V#2>
where K#1,V#1,K#2,V#2 are type-variables:
K#1 extends Object declared in class MyTreeMap.NavigableSubMap
V#1 extends Object declared in class MyTreeMap.NavigableSubMap
K#2 extends Object declared in class MyTreeMap
V#2 extends Object declared in class MyTreeMa...
La parte del código es ésta:
Código Java:
Ver originalclass NavigableSubMap<K,V> extends AbstractMap<K,V> implements SortedMap<K,V>{
final MyTreeMap<K,V> m;
final K lo, hi;
final boolean fromStart, toEnd;
final boolean loInclusive, hiInclusive;
NavigableSubMap(MyTreeMap<K, V> m,
boolean fromStart, K lo, boolean loInclusive,
boolean toEnd, K hi, boolean hiInclusive) {
if (!fromStart && !toEnd) {
if (m.compare(lo, hi) > 0)
} else {
if (!fromStart) // type check
m.compare(lo, lo);
if (!toEnd)
m.compare(hi, hi);
}
this.m = m;
this.lo = lo;
this.hi = hi;
this.fromStart = fromStart;
this.toEnd = toEnd;
this.loInclusive = loInclusive;
this.hiInclusive = hiInclusive;
}
final boolean tooLow
(Object key
) { if (!fromStart) {
int c = m.compare(key, lo);
if (c < 0 || (c == 0 && !loInclusive))
return true;
}
return false;
}
final boolean tooHigh
(Object key
) { if (!toEnd) {
int c = m.compare(key, hi);
if (c > 0 || (c == 0 && !hiInclusive))
return true;
}
return false;
}
final MyTreeMap.Entry<K,V> absLowest() {
MyTreeMap.Entry<K,V> e;
if(fromStart){
e = m.getFirstEntry();
} else if(loInclusive){
e = m.getCeilingEntry(lo);
} else {
e = m.getHigherEntry(lo);
}
return (e == null || tooHigh(e.getKey())) ? null : e;
}
final Entry<K,V> absHighFence() {
if(toEnd){
return null;
} else if(hiInclusive){
return m.getHigherEntry(hi);
} else {
return m.getCeilingEntry(hi);
}
}
@Override
public Set<Entry<K, V>> entrySet() {
return new EntrySetView();
}
@Override
public Comparator<? super K> comparator() {
return m.comparator;
}
@Override
public SortedMap<K, V> subMap(K fromKey, K toKey) {
return m.subMap(fromKey, toKey);
}
@Override
public SortedMap<K, V> headMap(K toKey) {
return m.headMap(toKey);
}
@Override
public SortedMap<K, V> tailMap(K fromKey) {
return m.tailMap(fromKey);
}
@Override
public K firstKey() {
return m.firstKey();
}
@Override
public K lastKey() {
return m.lastKey();
}
@Override
StringBuilder sb = new StringBuilder();
sb.append('{');
for (Entry<K,V> e : entrySet()) {
sb.append(e.getKey() == this ? "(this Map)" : e.getKey());
sb.append('=');
sb.append(e.getValue() == this ? "(this Map)" : e.getValue());
sb.append(',').append(' ');
}
return sb.append('}').toString();
}
class EntrySetView extends AbstractSet<Entry<K,V>> {
@Override
public Iterator<Entry<K, V>> iterator() {
//me subraya el error sólo en absLowest(),
return new SubMapEntryIterator(absLowest(), null);
}
@Override
public int size() {
if(fromStart && toEnd){
return m.size;
} else {
return size;
}
}
}
}
/*------------------------------------------------------------*/
abstract class SubMapIterator<T> implements Iterator<T> {
Entry<K,V> last;
Entry<K,V> next;
SubMapIterator(Entry<K,V> first, Entry<K,V> fence) {
last = null;
next = first;
fenceKey
= fence
== null ? new Object() : fence.
key; }
@Override
public boolean hasNext() {
return next != null && next.key != fenceKey;
}
public Entry<K,V> nextEntry() {
Entry<K,V> e = next;
next = successor(e);
last = e;
return e;
}
}
class SubMapEntryIterator extends SubMapIterator<Entry<K,V>> {
public SubMapEntryIterator(Entry<K, V> first, Entry<K, V> fence) {
super(first, fence);
}
@Override
public Entry<K, V> next() {
return nextEntry();
}
}
Y no me doy cuenta de dónde está el problema.
Espero sus respuestas y Saludos.