Generics Set with Enums

Problem space has been stripped down.

So I have the following interface that I cannot change.

public interface ItemInterface {
  Set<Enum<?>> getItems();
}

I have created the Items enum class

public enum Item{
  ITEM1, ITEM2, ITEM3 
}

So when I implement the method It does not compile.

public class ItemImpl implements ItemInterface {

    @Override
    public Set<Enum<?>> getItems() {
        Set<Item> vals = new HashSet<>();
        vals.add(Item.ITEM1);
        return vals;  
    }
}

The return type does not match and I get compile errors? Any ideas?

Answer

Simply declare the vals as Set<Enum<?>>:

public class ItemImpl implements ItemInterface {

    @Override
    public Set<Enum<?>> getItems() {
        Set<Enum<?>> vals = new HashSet<>();
        vals.add(Item.ITEM1);
        return vals;  
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *