How to prevent different enum values of same enum type from being added to a set?
For instance, I've made a Size enum:
public enum Size {
SMALL, MEDIUM, LARGE;
}
and added two different values into a Set of that type of Enum:
public class AttributesTestDrive {
public static void main(String[] args) {
Set<Size> sizes = new TreeSet<>();
sizes.add(Size.LARGE);
sizes.add(Size.MEDIUM);
sizes.stream().forEach(System.out::println);
}
}
How to override boolean equals(Object obj)
within Enum? Or what else would you do to solve this issue?
P.S. As I know enums are classes within Java.