'Replacing Duplicate values inside Set in Java instead of rejecting

I have to insert data inside Set in Java code.

To identify duplicates based on id in entity, I have overridden equals and hashcode method. Now, the values with duplicate id are rejected when trying to add inside Set.

Present behavior - Set rejects new data with id already existing in Set Expected: How to make Set replace the new duplicate values with the same id instead of rejecting? [similar to Map which replaces duplicate key ]

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source