Ver Mensaje Individual
  #14 (permalink)  
Antiguo 23/04/2017, 23:09
Avatar de detective_jd
detective_jd
 
Fecha de Ingreso: abril-2011
Ubicación: Salto
Mensajes: 437
Antigüedad: 13 años, 1 mes
Puntos: 6
Respuesta: Implementar TablaHash

Gracias por responder CalgaryCorpus verás al "HashMap" casero lo inicializo de está forma:

Código Java:
Ver original
  1. public class MyMap<K, V> implements Map<K,V>{
  2.     private int capacity;
  3.     private int size;
  4.     private static final int DEFAULT_SIZE = 4;
  5.     private Entry<K,V>[] table;    
  6.     public MyMap() {
  7.         this(DEFAULT_SIZE);
  8.     }
  9.     public MyMap(int xcapacity) {
  10.         if(xcapacity <= 0){
  11.             throw new IllegalArgumentException("Capacidad no permitida: " + capacity);
  12.         } else {
  13.             this.capacity = xcapacity;        
  14.         }
  15.         this.clear();
  16.     }
  17.     public MyMap(Map<? extends K, ? extends V> m) {
  18.         this.putAll(m);
  19.     }
  20.     @Override
  21.     public void clear() {
  22.         table = new Entry[capacity];
  23.         size = 0;
  24.     }
  25.     .............................
  26. }

Con respecto al tamaño si crece o no tengo este dilema:

Código Java:
Ver original
  1. public V put(K key, V value) {
  2.         if(key == null){
  3.             return null;
  4.         } else {
  5.             int index = getIndex(key); // aqui me da los dolores de cabeza xq me termina haciendo relajo en la última posición como viste en el anterior mensaje
  6.             System.out.println("indice -> "+ index);
  7.             Entry<K, V> pair = table[index];
  8.             if (pair != null && pair.getKey().equals(key)) {
  9.                 V oldValue = pair.getValue();
  10.                 pair.setValue(value);
  11.                 return oldValue;
  12.             }
  13.             setEntry(key, value, index);
  14.             return value;
  15.         }
  16.     }

Espero sus respuestas y saludos.