For simple data types, Set will work to avoid the duplicates using the Object class equals() and hashcode() methods, but to utilize the same behavior for the complex data types, we have to override the hashcode() and equals() methods as shown in the below example: