Ver Mensaje Individual
  #5 (permalink)  
Antiguo 03/06/2017, 22:42
Avatar de detective_jd
detective_jd
 
Fecha de Ingreso: abril-2011
Ubicación: Salto
Mensajes: 437
Antigüedad: 13 años
Puntos: 6
Respuesta: TablaHash y sus pasos

Hola CalgaryCorpus estuve viendo el error y el problema está con las malditas colisiones, y la solución está en lo que algunos llaman separate chaining.
Pero cómo lo implemento??

Código Java:
Ver original
  1. @Override
  2.     public V put(K key, V value) {
  3.         if(key != null){
  4.             int hash = hash(key,table.length);
  5.             for(Entry<K,V> e = table[hash]; e != null; e = e.next){
  6.                 if(e.getKey().equals(key)){
  7.                     V oldValue = e.getValue();
  8.                     e.setValue(value);
  9.                     return oldValue;
  10.                 }                
  11.             }
  12.             this.addEntry(key, value);
  13.             return value;                    
  14.         } else {
  15.             return null;
  16.         }
  17.     }
  18.     private void addEntry(K key, V value){
  19.         if(size >= table.length * 1/2){
  20.             Entry<K,V>[] tmp = table;
  21.             size = 0;
  22.             table = Arrays.copyOf(table, table.length * 2);
  23.             for(int i = 0; i < table.length; i++){
  24.                 table[i] = null;
  25.             }
  26.             for (Entry<K, V> e : tmp) {    
  27.                 if(e != null){
  28.                     put(e.getKey(),e.getValue());
  29.                 }
  30.             }            
  31.         }
  32.         int hash = hash(key,table.length);
  33.         table[hash] = new Entry(key, value,table[hash]);
  34.         size++;
  35.     }

Porque así tengo para agregar entradas al array, pero ¿cómo rayos implemento el separate chaning? porque con eso resuelvo ese problema.

Espero sus respuestas y saludos.