Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I use EnumSet<E> properly? 1

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
I have the following function:
Code:
	private static <E extends Enum<? super E>> boolean IsValueInEnum( E  value,
	                                                                E[]  values )
	{
//		EnumSet<E> eSet = EnumSet<E>.range( values[0], values[values.length] );
//		return eSet.contains( value );

		for ( E en : values )
		{
			if ( en == value )
			{
				return true;
			}
		}

		return false;
	}
I want to use the two commented out lines instead of the longer block of code below it.
If I uncomment the lines, it gives me an error about the '.' after EnumSet<E>. I haven't been using Generics (or Java) for very long, but I can't see what the problem is?
 
super?
Code:
 public class ExxSet
{
	private static <E extends Enum<E>> boolean isValueInEnum (E value, E [] values)
	{
		EnumSet<E> eSet = EnumSet.range (values [0], values [values.length]);
		return eSet.contains (value);
	}

	public enum Pop {SMITH, JOPLIN, JAGGER, ZAPPA}
	public enum Art {LICHTENSTEIN, GRAUBNER, DUCHAMPS, HRIDLICZKA}

	public static void main (String args[])
	{
		Pop [] females = {Pop.SMITH, Pop.JOPLIN};
		System.out.println ("Jagger in female = " + isValueInEnum (Pop.JAGGER, females));
		System.out.println ("Joplin in female = " + isValueInEnum (Pop.JOPLIN, females));
		// compile-fail:
		// System.out.println ("Duchamps in female = " + isValueInEnum (Art.DUCHAMPS, females));
	}
}

don't visit my homepage:
 
I thought I tried it with just <E extends Enum<E>>, but it seems to work. Thanks.
(I just tried it with Enum<? super E> since nothing else I tried seemed to be working.)

I'm still not 100% sure what <E extends Something<E>> actually means? How can E extend something that is already using E in its definition? Thoughts of infinite recursion come to mind when I stare at it too long. ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top