Tabela Hash em arquivo

2 respostas
programaçãojava
M

Pessoal, fiz um código para fazer uma tabela hash, ele funciona porém minha tabela está em memória principal, gostaria de saber como faço pra colocar minha tabela em arquivo?(não quero serializar a tabela)Como faço pra poder indexar os valores dentro do arquivo? Como tratar o arquivo para que ele represente a hash table? E as colisões? Vi em alguns sites na net que o arquivo da hash table poderia ter -1 representando os lugares ñ ocupados e que minha minha função hash me indicaria quanto bytes teria que andar dentro do arquivo para achar a chave e o seu valor nisso falando se tivessemos apenas inteiros mas tivessemos registros compostos por exemplo como; nome, idade e data de nascimento? Gostaria de uma idéia de como fazer isso?

public class Hash 
{    
    private int chave;
    private int valor;
    
    public Hash(int chave, int valor) 
    {
        this.chave = chave;
        this.valor = valor;
    }
    
    public Hash(int chave) 
    {
        this.chave = chave;
    }
    
    public int getChave() 
    {
        return chave;
    }
    
    public void setChave(int chave) 
    {
        this.chave = chave;
    }
    
    public int getValor() 
    {
        return valor;
    }
    
    public void setValor(int valor) 
    {
        this.valor = valor;
    }

}

_____

package exe3;


import java.util.ArrayList;
import java.util.List;

public class TabelaHash
{
    
    private List<List<Hash>> tabela = new ArrayList<List<Hash>>();
    private int m; //dimensao da tabela

    
    public TabelaHash(int m)
    {
        this.m = m;
    
        for (int i = 0; i < m; i++) 
        {
            List<Hash> lista = new ArrayList();
            tabela.add(lista);
        }
        
    }

    public List<List<Hash>> getTabela() {
        return tabela;
    }
    
    /*
    public void setTabela(List<List<Hash>> tabela) {
        this.tabela = tabela;
    }
    */
    
    public void divisao(Hash hash)
    {
        //metodo da divisao    
        int chave = hash.getChave();
        
        int i = chave % m;
        
        if(!chaveExiste(i,chave))
        {
            inserir(i,hash);
        }

    }
    
    public int dobra(int chave)
    {
        return 0;
    }
    
    public int multiplicacao(Hash hash)
    {
        char mul = (char)(hash.getChave() * hash.getChave());
        
        return 0;
    }
    
    public boolean chaveExiste(int indiceTabela,int chave)
    {    
        //crio uma referencia da lista que esta na tabela
        List lista = tabela.get(indiceTabela);
                            
        for (int j = 0; j < lista.size(); j++) 
        {
                if(lista.get(j).equals(chave))
                { 
                        //chave já existe
                        System.out.println("Chave:  "+chave+" ja existe na tabela, nao vou inserir");
                        return true;
                }
        }
                // chave nao existe 
                return false;
    }
        
    public void inserir(int indiceTabela,Hash hash)
    {
            int chave = hash.getChave();
            //crio uma referencia da lista que esta na tabela
            List lista = tabela.get(indiceTabela);
            
            colisao(lista,chave);
            
            //adiciono chave na lista    
            System.out.println("Inserindo a chave:  "+chave+" no indice:  "+indiceTabela);
            lista.add(hash);
        
    }
    
    public void colisao(List lista,int chave)
    {
        if(!lista.isEmpty())
        {
            System.out.println("Ocorreu colisao para a chave:  "+chave);
        }
    }
    
    public void imprimirTabela()
    {
        System.out.println();
        System.out.println("Imprimindo tabela hash final:");
        
        for (int i = 0; i < tabela.size(); i++) 
        {
            System.out.println("elementos da posicao "+i+" da tabela:");
            for (int j = 0; j < tabela.get(i).size(); j++) 
            {
                if(!tabela.get(i).isEmpty())
                {    
                    System.out.println(tabela.get(i).get(j).getChave());
                    
                }
                
            }
            
            
        }
        
    }
}

______

package exe3; 
 
import java.util.Scanner; 
 
import util.Conversao; 
 
public class Main { 
 
    public static void main(String[] args) { 
         
        int opcao; 
        String chaves; 
        System.out.println("Escolha o metodo: 1-Metodo da Divisao " 
                            + "2-Metodo da Dobra " 
                            + "3-Metodo da Multiplicaçao"); 
         
        Scanner teclado1 = new Scanner(System.in); 
        Scanner teclado2 = new Scanner(System.in); 
        opcao = teclado1.nextInt(); 
         
        System.out.println("Digite as chaves separadas por espaco:"); 
        chaves = teclado2.nextLine(); 
         
        String[] chavesString = chaves.split(" "); 
        int[] chavesInt = Conversao.charInt(chavesString); 
         
        TabelaHash t1 = new TabelaHash(3); 
         
        int i=0; 
        switch (opcao)  
        { 
            case 1: 
                while(i < chavesInt.length) 
                { 
                    //crio objeto do tipo hash(chave e valor) por enquanto do uso a chave 
                    Hash hash = new Hash(chavesInt[i]); // Hash possui dois construtores 1 somente com a chave 
                    t1.divisao(hash);                    // e outro  com chave e valor 
                    i++; 
                } 
                 
                t1.imprimirTabela(); 
                break; 
                 
            case 2: 
                while(i < chavesInt.length) 
                { 
                     
                    Hash hash = new Hash(chavesInt[i]); 
                    //t1.dobra(hash); 
                    i++; 
                } 
             
                break; 
            case 3: 
                while(i<chaves.length()) 
                { 
                     
                    Hash hash = new Hash(chavesInt[i]); 
                    //t1.multiplicacao(hash); 
                    i++; 
                } 
     
                break; 
 
            default: 
                break; 
        } 
         
    } 
}

2 Respostas

P

Ola

parte disso ja não foi respondido aqui?

M

não.

Criado 3 de novembro de 2016
Ultima resposta 4 de nov. de 2016
Respostas 2
Participantes 2