Oi!
eu estava fazendo uns exercicios da faculdade pediram para eu gerar o hash da minha classe, como eu não entendi muito bem
eu pedi para o netbeans gerar para mim.
Até td bem ele gerous mas não funcionou.
public class Aluno implements Comparable{
private String nome, turno, semestre;
private int matricula;
public Aluno() {
setNome("");
setTurno("");
setSemestre("");
setMatricula(0);
}
//gets e sets
@Override
public int hashCode() {
int hash = 5;
hash = 17 * hash + (this.nome != null ? this.nome.hashCode() : 0);
hash = 17 * hash + (this.turno != null ? this.turno.hashCode() : 0);
hash = 17 * hash + (this.semestre != null ? this.semestre.hashCode() : 0);
hash = 17 * hash + this.matricula;
return hash;
}
essa eh minha classe abaixo como eu testei.
public class TesteHashSet {
public static void main(String[] args){
Set<Aluno> conjunto = new HashSet<Aluno>();
Aluno a1 = new Aluno();
a1.setNome("Renato");
a1.setTurno("noite");
a1.setMatricula(95);
a1.setSemestre("semestre");
Aluno a2 = new Aluno();
a2.setNome("Renato");
a2.setTurno("noite");
a2.setMatricula(95);
a2.setSemestre("semestre");
conjunto.add(a1);
conjunto.add(a2);
System.out.println(conjunto);
}
}
alguem me explica pq não funcionou.
muito obrigado =D