Java class object not equal even they are equal

Hey i have two java class object, whose key and values are same but when I check ob1.equals(obj2) its return false.

here is the code :

   Category expected = new Category("01","lorem","custom");


  ResponseEntity<List<LinkedHashMap>> response = restTemplate.exchange("/api/categories", HttpMethod.GET,
                        null, new ParameterizedTypeReference<List<LinkedHashMap>>() {});

LinkedHashMap result = response.getBody().get(0); // which is same as expected object

//check if equals

 private boolean areEqual(LinkedHashMap result, Category expected) {
        String catId = (String) obj.get("category_id"); //is 01
        String name = (String) obj.get("category_name"); // is lorem
        String sec = (String) obj.get("section_name");   // is custom
        DefaultCategory temp = new Category(catId, name, sec);
        return temp.equals(expected);  //<--------- returning false, even they are equal
    }

The api return this category

 @GetMapping("categories")
    public ResponseEntity<List<Category>> getDefaultCategories() {
        List<Category> categories = new ArrayList();
        categories.add(new Category("01","lorem","custom"));
        return new ResponseEntity<>(categories, HttpStatus.OK);
    }


Read more here: https://stackoverflow.com/questions/65727059/java-class-object-not-equal-even-they-are-equal

Content Attribution

This content was originally published by user8462556 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: