[Bluej-discuss] Enum types - what does enumeration mean..

Bryan J. Higgs bhiggs at rivier.edu
Sat Mar 25 16:04:42 GMT 2006


Gordon --

My interest was piqued by your questions, and I decided to do some
learning on the subject of Java enums.

Here are the results of my (very) short investigation, for what they are
worth.  My point is not really to answer your questions, but to show
some usage of Java enums to provide a context which people can understand.

------------------------------------------------------------------------

package play;

enum Coin
{
  PENNY(1), NICKEL(5), DIME(10), QUARTER(25), HALF_DOLLAR(50), DOLLAR(100);
 
  Coin(int value)
  { m_value = value; }
 
  public int getValue()
  { return m_value; }
 
  private final int m_value;
}

------------------------------------------------------------------------

package play;

import java.util.Comparator;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.TreeSet;

public class TestEnum
{
  public static void main(String[] args)
  {
    int coinCount = Coin.values().length;
    Coin[] coins = new Coin[coinCount];
    int i = coinCount - 1;
   
    for (Coin c : Coin.values())
    {
      coins[i--] = c;
      System.out.println("A " + c + " is worth " + c.getValue() + " cents");
    }
   
    for (Coin c : coins)
    {
      if (c.compareTo(Coin.DIME) < 0)
        System.out.println(c + " < DIME");
      else if (c.equals(Coin.DIME))
        System.out.println(c + " == DIME");
      else
        System.out.println(c + " > DIME");
    }
   
    EnumSet<Coin> eSet = EnumSet.allOf(Coin.class);
    System.out.println("Contents of EnumSet<Coin>:");
    Iterator<Coin> iter = eSet.iterator();
    while (iter.hasNext())
    {
      System.out.println(iter.next());
    }
   
    TreeSet<Coin> set = new TreeSet<Coin>( new ReverseComparator() );
    for (Coin c : Coin.values())
    {
      set.add(c);
    }
    System.out.println("Contents of TreeSet:");
    for (Coin c : set)
    {
      System.out.println(c);
    }
  }
}

class ReverseComparator implements Comparator<Coin>
{
  public int compare(Coin c1, Coin c2)
  {
    return -c1.compareTo(c2);
  }

}

------------------------------------------------------------------------

I am aware that the compareTo() method used to compare the above Coins
is based not on the value I added, but on the order of declaration of
the enum instances.

So:

    * As you said, there is a way of doing comparisons, albeit using
      compareTo() rather than the comparison operators. Since the Java
      folks implemented enums as instances of a class, it makes sense
      that comparison operators wouldn't work.
      One could also argue that they could have implemented Strings that
      could use the comparison operators, which certainly would have
      been more convenient in many ways.
      The Java folks could have implemented enums in a way that is
      similar to C/C++ enums.  I'm very glad they did not.

    * There is a way of obtaining the Set behaviour, using EnumSet,
      which provides you with the ability of having an iterator, if you
      wish.  It certainly isn't direct, but it's possible.  (Again, I
      think you were aware of this.)

I hope the above helps people to understand some of the issues...

Regards

Bryan



More information about the bluej-discuss mailing list