// Copyright (c) Keith D Gregory, all rights reserved
package com.kdgregory.example.enums;
import java.util.EnumSet;
import java.util.Set;
/**
* Demonstrates that EnumSet
maintains type information, which
* makes it more useful than a parameterized HashMap
*/
public class EnumSetIsTypesafe
{
enum MyEnum { FOO, BAR, BAZ }
public static void main(String[] argv)
throws Exception
{
// note that I'm explicitly messing with parameterization
Set set = EnumSet.noneOf(MyEnum.class);
// and this will give you a ClassCastException, which HashSet won't
set.add("something not in the enum");
}
}