fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class Mercadoria {
  8. private String nome;
  9.  
  10. Mercadoria(String nome) {
  11. this.nome = nome;
  12. }
  13.  
  14. public String getNome() {
  15. return this.nome;
  16. }
  17.  
  18. @Override
  19. public int hashCode() {
  20. return this.getNome().hashCode();
  21. }
  22.  
  23. @Override
  24. public boolean equals(Object o) {
  25. return this.getNome().equals(((Mercadoria) o).getNome());
  26. }
  27. }
  28.  
  29. /* Name of the class has to be "Main" only if the class is public. */
  30. class Ideone
  31. {
  32. public static void main (String[] args) throws java.lang.Exception {
  33. Set<Mercadoria> lista = new HashSet<Mercadoria>();
  34. Mercadoria mercadoria1 = new Mercadoria("dado");
  35. Mercadoria mercadoria2 = new Mercadoria("dado");
  36.  
  37. System.out.println(lista.add(mercadoria1));
  38. System.out.println(lista.add(mercadoria2));
  39. }
  40. }
Success #stdin #stdout 0.06s 50832KB
stdin
Standard input is empty
stdout
true
false