Dúvida HashMap

8 respostas
L

oi pessoal
criei uma tabela hash através de um HashMap… mas o problema é que cada componente precisa carregar duas informações em numerico ao invés de uma. Tem algum jeito de fazer isso ou vou ter que fazer na “gambiarra”?
Obrigado, e abraços

8 Respostas

F

Cara se foi isso q eu entendi é so fazer

Map<Integer, Integer> mapa_hash = new HashMap<Integer, Integer>();

Mas se não for mal ae, tentei.
Flw.

L

Não, teria que ser algo do tipo
tabelaHash = new HashMap <String, Double, Double>();
o que não funciona… Tem algum jeito de fazer isso sem ser na gambiarra?

Lavieri
/**
 * Um par de objetos
 * @param <F> first (primeiro) elemento do par
 * @param <S> second (segundo) elemento do par
 */
public class Pair <F,S> {
   private final F first
   private final S second
   
   public static <F,S> Pair<F,S> valueOf(F first, S second) {
      return new Pair(first,second);
   }
   
   /**
    * Cria um par de objetos, onde a ordem é idenferente para comparação
    */
   public Pair(F first, S second) {
      this.first = first;
      this.second = second;
   }
   /**
    * Recupera o primeiro elemento do par
    */
   public F getFirst() {
      return first;
   }
   /**
    * Recupera o segundo elemento do par
    */   
   public S getSecond() {
      return second;
   }
   //depois mostro como montar o equals e hashcode (que é importante)
}

ai depois só fazer

Map<String, Pair<Double,Double>> = new HashMap<String, Pair<Double,Double>>();

ai só fazer

map.put("string",Pair.valueOf(10.35d,11.33d));
L
public class TabelaHashFrequencia {
    private HashMap <String, Par><Double,Double>> tabelaHash;

    public TabelaHashFrequencia() {
        tabelaHash = new HashMap<String, Par><Double,Double>>();
    }
...
}
essa parte não está compilando... illegal start of expression, not a statement...
L

Uma outra opção seria criar duas tabelas hash normais né, com os mesmos valores em String…

S
Tenta assim:
public class TabelaHashFrequencia {
    private HashMap <String, Par<Double,Double>> tabelaHash;

    public TabelaHashFrequencia() {
        tabelaHash = new HashMap<String, Par<Double,Double>>();
    }
...
}

voce colocou um ">" a mais

m4tr1x

Pessoal por favor aluem m ajude no meu topico… nimguem ajud :frowning:

http://www.guj.com.br/posts/list/127652.java
abra;os vlw

Lavieri
luisera:
public class TabelaHashFrequencia {
    private HashMap <String, Par><Double,Double>> tabelaHash;

    public TabelaHashFrequencia() {
        tabelaHash = new HashMap<String, Par><Double,Double>>();
    }
...
}
essa parte não está compilando... illegal start of expression, not a statement...

é não ta funcionando, pq quando a pessoa envia um código generico, ele sempre acaba incluindo > a mais...

eu editei o código la... e agora ta correto...

a idéia é ter um par de objetos juntos em um único

//assim vc cria um mapa
Map<String, Pair<Double,Double>> = new HashMap<String, Pair<Double,Double>>();
//assim vc adciona um valor
map.put("string",Pair.valueOf(10.35d,11.33d));

//assim vc pega um valor
Pair<Double,Double> valor = map.get("string");

//primeiro valor
valor.getFirst();
//segundo valor
valor.getSecond();

.............

o código novo do objeto ai abaixo, não me xinga por meu ingles...

/**
 * A pair of two elements
 * @author Tomaz Lavieri
 * @param <F> The type of first element of the pair.
 * @param <S> The type of second element of the pair.
 */
public class Pair<F,S> {
    /**
     * The first element of the pair.
     */
    private final F first;
    /**
     * The second element of the pair.
     */
    private final S second;
    /**
     * Return a {@link Pair} containing the <tt>first</tt> and <tt>second</tt>
     * elements send on arguments.
     * @param <F> The type of first element of the pair.
     * @param <S> The type of second element of the pair.
     * @param first The first element of the pair.
     * @param second The second element of the pair.
     * @return  a {@link Pair} containing the <tt>first</tt> and <tt>second</tt>
     *          elements.
     */
    public static <F,S> Pair<F,S> valueOf(F first, S second) {
        return new Pair<F,S>(first, second);
    }
    /**
     * Create a {@link Pair} containing the <tt>first</tt> and <tt>second</tt>
     * elements send on arguments.
     * @param first The first element of the pair.
     * @param second The second element of the pair.
     */
    public Pair(F first, S second) {
        this.first = first;
        this.second = second;
    }
    /**
     * Get the first element of this Pair
     * @return The first element of the pair.
     */
    public F getFirst() {
        return first;
    }
    /**
     * Get the second element of this Pair.
     * @return The second element of the pair.
     */
    public S getSecond() {
        return second;
    }
    /**
     * Test if this Pair contains the same elements of the other Pair, no matter
     * the order of elements.<br>
     * <br>
     * The test will return <tt>true</tt> in two cases:<br>
     * <ol><li>when the first element of this Pair is equals to the first of the
     * order Pair and the second element of this Pair is equals to the second of
     * the other Pair.</li>
     * <li>when the first element of this Pair is equals to the second of the
     * other Pair and the second element of this Pair is equals to the first of
     * the other Pair.</li></ol>
     * Otherwise will return <tt>false</tt>.
     * @param other The other Pair
     * @return  <tt>true</tt> if this Pair contains the same elements of the
     *          other Pair, no matter the order.<br>
     *          <tt>false</tt> otherwise.
     */
    public boolean equalsIgnoreOrder(Pair other) {
        if (isEquals(first, other.first))
            return isEquals(second, other.second);
        else
            return isEquals(second, other.first) && isEquals(first, other.second);
    }
    /**
     * Test if this {@link Pair} is equals to another {@link Pair} with the same
     * <tt>first</tt> and <tt>second</tt> elements.<br>
     * <br>
     * The order of elements in the {@link Pair} is important on the test, and
     * this test will return <tt>true</tt> only when the first element of this
     * Pair is equals to the first of the order Pair and the second element of
     * this Pair is equals to the second of the other Pair.
     * @param o The other object
     */
    public @Override boolean equals(Object o) {
        return o instanceof Pair && equals((Pair)o);
    }
    
    public @Override int hashCode() {
        int hash = 7;
        hash = 79 * hash + (first != null ? first.hashCode() : 0);
        hash = 79 * hash + (second != null ? second.hashCode() : 0);
        return hash;
    }
    /**
     * Internal test of equals.<br>
     * <br>
     * The <tt>other</tt> argument can not be sent <tt>null</tt>
     * @param other Pair to test.
     * @return  <tt>true</tt> if this object is the same as the obj argument;
     *          <tt>false</tt> otherwise.
     */
    private boolean equals(Pair other) {
        assert(other != null) : "other is never sent null.";
        return isEquals(first, other.first) && isEquals(second, other.second);
    }
    /**
     * Verify if one object is equals to other object, the test include a test
     * of <tt>null</tt>s objects.<br>
     * <br>
     * The test return <tt>true</tt> when all two objects are equals, or
     * <tt>null</tt>s
     * @param one object to test
     * @param other object to test
     * @return  <tt>true</tt> if this object is the same as the obj argument;
     *          <tt>false</tt> otherwise.
     */
    private boolean isEquals(Object one, Object other) {
        return (one == null) ? one == other : one.equals(other);
    }
}
Criado 23 de maio de 2009
Ultima resposta 24 de mai. de 2009
Respostas 8
Participantes 5