Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cadastro;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.List;
- public class MapaEspalhamento {
- private ArrayList<List<Associacao>> tabela = new ArrayList<List<Associacao>>();
- public MapaEspalhamento() {
- for (int i = 0; i < 100; i++) {
- this.tabela.add(new LinkedList<Associacao>());
- }}
- private int calculaIndiceDaTabela(String cpf) {
- return Math.abs(cpf.hashCode()) % this.tabela.size();
- }
- public boolean contemCpf(String cpf) {
- int indice = this.calculaIndiceDaTabela(cpf);
- List<Associacao> lista = this.tabela.get(indice);
- for (int i = 0; i < lista.size(); i++) {
- Associacao associacao = lista.get(i);
- if (associacao.getCpf().equals(cpf)) {
- return true;
- }
- }
- return false;
- }
- public void remove(String cpf) {
- int indice = this.calculaIndiceDaTabela(cpf);
- List<Associacao> lista = this.tabela.get(indice);
- for (int i = 0; i < lista.size(); i++) {
- Associacao associacao = lista.get(i);
- if (associacao.getCpf().equals(cpf)) {
- lista.remove(i);
- return;
- }
- }
- throw new IllegalArgumentException("A chave não existe");
- }
- public void adiciona(String cpf, String pessoa) {
- if (this.contemCpf(cpf)) {
- this.remove(cpf);
- }
- int indice = this.calculaIndiceDaTabela(cpf);
- List<Associacao> lista = this.tabela.get(indice);
- lista.add(new Associacao(cpf, pessoa));
- }
- public String pega(String cpf) {
- int indice = this.calculaIndiceDaTabela(cpf);
- List<Associacao> lista = this.tabela.get(indice);
- for (int i = 0; i < lista.size(); i++) {
- Associacao associacao = lista.get(i);
- if (associacao.getCpf().equals(cpf)) {
- return associacao.getPessoa();
- }
- }
- throw new IllegalArgumentException("A chave não existe");
- }
- public List<List<Associacao>> pegaTodas(){
- List<List<Associacao>> associacoes= new ArrayList<List<Associacao>>();
- for(int i=0;i<this.tabela.size();i++){
- associacoes.addAll(this.tabela.get(i));
- }
- return associacoes;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment