2

I have a basic SpringBoot 2.0.6.RELEASE app. Using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR with a restful architecture I have this object:

public class Menu  implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    private Long id;

     @Override
        public int hashCode() {
            return (int) (id ^ (id >>> 32));
        }



    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Menu menu = (Menu) o;

        return id == menu.id;
    }
...
}

I also have this piece of code in the controller:

List<Menu> favoriteMenus = new ArrayList<Menu>();

        favoriteMenus.addAll(user.getFavoriteMenus());
        favoriteMenus.addAll(applicationProfileService
                .menusByProfile(user.getApplicationSetup().getApplicationProfile().getProfileKey()));

        favoriteMenus = 
                favoriteMenus
                    .stream()
                    .distinct()
                    .collect(Collectors.toList());

but despite of distinct() there are repeated menus in the list

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
Nuñito Calzada
  • 4,394
  • 47
  • 174
  • 301

1 Answers1

3

You're testing the reference equality of id instead of its value equality (more on that e.g. here), and id is a Long:

The Long class wraps a value of the primitive type long in an object.

Change id == menu.id to id.equals(menu.id), and it should work (provided there are no other bugs here).

Tomasz Linkowski
  • 4,386
  • 23
  • 38