Ver Mensaje Individual
  #19 (permalink)  
Antiguo 18/07/2018, 21:24
Avatar de detective_jd
detective_jd
 
Fecha de Ingreso: abril-2011
Ubicación: Salto
Mensajes: 437
Antigüedad: 13 años
Puntos: 6
Respuesta: Balanceos con Árboles Hash

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 original
  1. class NavigableSubMap<K,V> extends AbstractMap<K,V> implements SortedMap<K,V>{
  2.         final MyTreeMap<K,V> m;
  3.         final K lo, hi;
  4.         final boolean fromStart, toEnd;
  5.         final boolean loInclusive, hiInclusive;
  6.         NavigableSubMap(MyTreeMap<K, V> m,
  7.                 boolean fromStart, K lo, boolean loInclusive,
  8.                 boolean toEnd, K hi, boolean hiInclusive) {
  9.             if (!fromStart && !toEnd) {
  10.                 if (m.compare(lo, hi) > 0)
  11.                     throw new IllegalArgumentException("fromKey > toKey");
  12.             } else {
  13.                 if (!fromStart) // type check
  14.                     m.compare(lo, lo);
  15.                 if (!toEnd)
  16.                     m.compare(hi, hi);
  17.             }
  18.             this.m = m;
  19.             this.lo = lo;
  20.             this.hi = hi;
  21.             this.fromStart = fromStart;
  22.             this.toEnd = toEnd;
  23.             this.loInclusive = loInclusive;
  24.             this.hiInclusive = hiInclusive;
  25.         }        
  26.         final boolean tooLow(Object key) {
  27.             if (!fromStart) {
  28.                 int c = m.compare(key, lo);
  29.                 if (c < 0 || (c == 0 && !loInclusive))
  30.                     return true;
  31.             }
  32.             return false;
  33.         }
  34.         final boolean tooHigh(Object key) {
  35.             if (!toEnd) {
  36.                 int c = m.compare(key, hi);
  37.                 if (c > 0 || (c == 0 && !hiInclusive))
  38.                     return true;
  39.             }
  40.             return false;
  41.         }
  42.         final MyTreeMap.Entry<K,V> absLowest() {
  43.             MyTreeMap.Entry<K,V> e;
  44.             if(fromStart){
  45.                 e = m.getFirstEntry();
  46.             } else if(loInclusive){
  47.                 e = m.getCeilingEntry(lo);
  48.             } else {
  49.                 e = m.getHigherEntry(lo);
  50.             }
  51.             return (e == null || tooHigh(e.getKey())) ? null : e;
  52.         }
  53.         final Entry<K,V> absHighFence() {
  54.             if(toEnd){
  55.                 return null;
  56.             } else if(hiInclusive){
  57.                 return m.getHigherEntry(hi);
  58.             } else {
  59.                 return m.getCeilingEntry(hi);
  60.             }
  61.         }
  62.         @Override
  63.         public Set<Entry<K, V>> entrySet() {
  64.             return new EntrySetView();
  65.         }
  66.         @Override
  67.         public Comparator<? super K> comparator() {
  68.             return m.comparator;
  69.         }
  70.         @Override
  71.         public SortedMap<K, V> subMap(K fromKey, K toKey) {
  72.             return m.subMap(fromKey, toKey);
  73.         }
  74.         @Override
  75.         public SortedMap<K, V> headMap(K toKey) {
  76.             return m.headMap(toKey);
  77.         }
  78.         @Override
  79.         public SortedMap<K, V> tailMap(K fromKey) {
  80.             return m.tailMap(fromKey);
  81.         }
  82.         @Override
  83.         public K firstKey() {
  84.             return m.firstKey();
  85.         }
  86.         @Override
  87.         public K lastKey() {
  88.             return m.lastKey();
  89.         }
  90.         @Override
  91.         public String toString(){
  92.             StringBuilder sb = new StringBuilder();
  93.             sb.append('{');
  94.             for (Entry<K,V> e : entrySet()) {
  95.                 sb.append(e.getKey() == this ? "(this Map)" : e.getKey());
  96.                 sb.append('=');
  97.                 sb.append(e.getValue() == this ? "(this Map)" : e.getValue());
  98.                 sb.append(',').append(' ');
  99.             }
  100.             return sb.append('}').toString();
  101.         }
  102.         class EntrySetView extends AbstractSet<Entry<K,V>> {
  103.             @Override
  104.             public Iterator<Entry<K, V>> iterator() {
  105.                 //me subraya el error sólo en absLowest(),
  106.                 return new SubMapEntryIterator(absLowest(), null);
  107.             }
  108.             @Override
  109.             public int size() {
  110.                 if(fromStart && toEnd){
  111.                     return m.size;
  112.                 } else {
  113.                     return size;
  114.                 }
  115.             }        
  116.         }
  117.     }
  118.     /*------------------------------------------------------------*/    
  119.     abstract class SubMapIterator<T> implements Iterator<T> {
  120.         Entry<K,V> last;
  121.         Entry<K,V> next;
  122.         final Object fenceKey;
  123.         SubMapIterator(Entry<K,V> first, Entry<K,V> fence) {
  124.             last = null;
  125.             next = first;
  126.             fenceKey = fence == null ? new Object() : fence.key;
  127.         }
  128.         @Override
  129.         public boolean hasNext() {
  130.             return next != null && next.key != fenceKey;
  131.         }
  132.         public Entry<K,V> nextEntry() {
  133.             Entry<K,V> e = next;
  134.             next = successor(e);
  135.             last = e;
  136.             return e;
  137.         }        
  138.     }
  139.     class SubMapEntryIterator extends SubMapIterator<Entry<K,V>> {
  140.         public SubMapEntryIterator(Entry<K, V> first, Entry<K, V> fence) {
  141.             super(first, fence);
  142.         }        
  143.         @Override
  144.         public Entry<K, V> next() {
  145.             return nextEntry();
  146.         }        
  147.     }

Y no me doy cuenta de dónde está el problema.

Espero sus respuestas y Saludos.
__________________
Si te interesa, visita mi perfil de Linkedin. Gracias