fork download
  1. import java.util.HashSet;
  2. import java.util.Objects;
  3. import java.util.Set;
  4.  
  5. /**
  6.  * @author Obicere
  7.  */
  8. public class Main {
  9.  
  10. public static void main(final String[] args) {
  11.  
  12. final Set<PlacesInfo> placeId = new HashSet<>();
  13.  
  14. placeId.add(new PlacesInfo("Foo"));
  15. placeId.add(new PlacesInfo("Bar"));
  16. placeId.add(new PlacesInfo("Foo"));
  17.  
  18. System.out.println(placeId);
  19. }
  20.  
  21. private static class PlacesInfo {
  22.  
  23. private final String placeId;
  24.  
  25. // Assuming the class structure is something like this...
  26.  
  27. public PlacesInfo(final String placeId) {
  28. this.placeId = placeId;
  29. }
  30.  
  31. @Override
  32. public int hashCode() {
  33. int hash = 5;
  34. hash = 97 * hash + Objects.hashCode(this.placeId);
  35. return hash;
  36. }
  37.  
  38. @Override
  39. public boolean equals(Object obj) {
  40. if (obj == null) {
  41. return true;
  42. }
  43. if (this.getClass() != obj.getClass()) {
  44. return false;
  45. }
  46. final PlacesInfo other = (PlacesInfo) obj;
  47. if (!Objects.equals(this.placeId, other.placeId)) {
  48. return false;
  49. }
  50. return true;
  51. }
  52.  
  53. // Added toString to make printing easier on the eyes
  54. @Override
  55. public String toString() {
  56. return placeId;
  57. }
  58. }
  59. }
  60.  
Success #stdin #stdout 0.07s 381248KB
stdin
Standard input is empty
stdout
[Foo, Bar]