-2

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.

LowLevel
  • 1,085
  • 1
  • 13
  • 34
  • 2
    So you want to be able to add, e.g. `Size.LARGE` but not `Size.MEDIUM`? Why? – Tunaki Dec 30 '15 at 13:54
  • 1
    ...and what do you want the set to contain? the first or second item? – Sharon Ben Asher Dec 30 '15 at 13:56
  • Create either 1) your own `class` that is `enum` like or 2) your own `Set` implementation. Neither `enum` nor `Set` is designed to behave in this way. – Boris the Spider Dec 30 '15 at 13:56
  • If you want a unique value why do you use a set at all? A simple field / variable would be enough. – Thomas Kläger Dec 30 '15 at 13:57
  • Dear Tunaki, I've posted two different questions because I didn't want to merge two different questions within a single topic. You've marked the other question as a duplicate, but I'm not agree with you, because the question you've point to, doesn't answer my question. – LowLevel Dec 30 '15 at 13:57
  • @ThomasKläger it is often useful to have a field that is `Set` like - for example `Optional` is `Set` like - it is either the singleton `Set` or empty. – Boris the Spider Dec 30 '15 at 13:58
  • You just cant override equals for ENUM. For details : http://stackoverflow.com/questions/2964704/why-java-does-not-allow-overriding-equalsobject-in-an-enum – Ajinkya Dec 30 '15 at 13:58
  • If I begin to explain why I need to add one value to a set my post will become as large as my project. – LowLevel Dec 30 '15 at 13:58
  • 2
    @LowLevel this is obviously an XY problem. You are trying to do things with a `Set` and an `enum` that neither was intended to do - please explain what it is that you want to do. – Boris the Spider Dec 30 '15 at 14:00
  • I will edit my question. – LowLevel Dec 30 '15 at 14:02
  • @Tunaki I hope I could telll you why – LowLevel Dec 30 '15 at 14:18
  • 3
    Okay, this is now a code dump. You need to post an [SSCCE](http://sscce.org/), which this certainly isn't. We need **words** to explain what you want to do and a **short** example to demonstrate. What you have done is dump a whole lot of code with **no** explanation at all. – Boris the Spider Dec 30 '15 at 14:19
  • You are not here for explanations, but to report users. I have never understood this mentality, but keep going; you'll ever rich your goal. If it makes you happy. I'm happy too. We could make each other happy. – LowLevel Dec 30 '15 at 14:22
  • So - what I understand: You want a collection of data, where every single "type" of selection can be added *at most once* with *one if the enum values* as it's value. But why are you even considering a Set for this? – Jan Dec 30 '15 at 14:41
  • @Jan It hasn't to be a set. I just didn't want to make a property per attribute to make the class flexible if any attribute is added in the future. In other words, if something changes in the future I do not want to touch the class (that contains attributes). Instead I wanted to make a new enum implementing Attribute-interface. And I just tried to prevent a attribute from being input more than once, because otherwise you get a wrong HTML output. In that (wrong) case you may get which is not allowed. – LowLevel Dec 30 '15 at 14:45

1 Answers1

2

How about a map instead of a Set?

Map <Class,Attribute> attributes;

Should do the job. You could have one Size:

attributes.put(Size.class, Size.LARGE);

and if you put it into a class you could even do some magic on retrieval:

public <T> T get (T defaultValue) {
  if (attributes.containsKey (defaultValue.getClass ())) {
     return (T) attributes.get (defaultValue.getClass ());
   } else  {
     return defaultValue;
   }
}
Jan
  • 13,738
  • 3
  • 30
  • 55