Skip to content
Advertisement

Tag: equals

Add unique elements in HashSet based on attributes of framework provided non-editable object classes

I am trying to generate a HashSet containing unique Employee instances. Uniqueness should be established based on the object properties. The problem is that I end up with having duplicates. Note that Employee class is provided by a framework, and it’s not possible to provide custom implementations for equals() and hashCode(). Employee class: This would result in a Set mapped

Compare two objects excluding some fields – Java

I need to compare two objects of the same class excluding some fields. How can i find if the two objects of the above class are equal excluding createdAt and updatedAt values? Since there are a lot of fields in this class, i don’t want to compare each of them one by one. Please don’t give AssertJ’s recursive comparison solution

How can UserDefined class be a key of hashmap if hashCode() & equals() return same value

I am trying to implement HashMap with UserDefined class as Key, i am successfull even when I implement both hashCode() (returns 0 for every object) & equals() (returns false for every object). My Code output:- Can anyone let me know the reason how it’s working Answer This is happening because equals is returning false. The equals/hashCode contract is completely broken.

problem with equals() method when used after toString() method

When I tried to take two strings as input to a function and check whether they are anagrams, I am getting the wrong output. I have written the following code. The sample input I have taken is s = “anagram” and t = “nagaram” . When checked, both the char arrays are printing the same value, i.e. But my output

Why is casting in equals necessary?

I’m doing the Mooc course and it’s teaching us how to compare objects from made up classes like “Person” and see if they are equal to each other. They give us the following code: } For the equals method, I get that they compare using == first to check if it’s the same location. Next they see if the compared

Advertisement