PA: Tabelas Hash
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pa.exercicio.tabelahash;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @author ta2
*/
public class PaExercicioTabelaHash {
private static void adicionar(
Map
String valor)
{
// verificar se a lista jah estah istanciada
// na posição definida pela chave
if (hash.get(chave) == null)
{
hash.put(chave, new ArrayList
}
hash.get(chave).add(valor);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// Problema
// Entrada: informar um conjunto de nomes de cidades
// Saída: Exibir as cidades organizadas por estado
String bh = "Belo Horizonte";
String mg = "Minas Gerais";
String cont = "Contagem";
String sl = "Santa Luzia";
String rj = "Rio de Janeiro";
String cf = "Cabo Frio";
String sbc = "São Bernardo do Campo";
String mn = "Manaus";
// criando a tabela hash
Map
new HashMap
adicionar(hash, mg, bh);
adicionar(hash, mg, sl);
adicionar(hash, mg, cont);
adicionar(hash, rj, rj);
adicionar(hash, rj, cf);
adicionar(hash, "São Paulo", sbc);
adicionar(hash, "Amazonas", mn);
for (String chave : hash.keySet())
{
System.out.println(chave +
"[" + hash.get(chave).size()+
"]: " + hash.get(chave));
}
if (hash.get("PB") == null)
{
System.out.println("Nao existe");
}
}
}

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home