What is the difference between this:
public enum Direction { NORTH(90), EAST(0), SOUTH(270), WEST(180); private final int degree_; private Direction(int degree) { degree_ = degree; } public int getDegree() { return degree_; } public static void main(String[] args) { System.out.println(NORTH.getDegree()); } }
and this:
public enum Direction { NORTH, EAST, SOUTH, WEST; public static void main(String[] args) { EnumMap<Direction, int> directionToDegree = new EnumMap<Direction, int>(Direction.class); directionToDegree.put(NORTH, 90); directionToDegree.put(EAST, 0); directionToDegree.put(SOUTH, 270); directionToDegree.put(WEST, 180); System.out.println(directionToDegree.get(NORTH)); } }
Is it easier to read? Is it even more flexible? Is it used more traditionally? Do you run faster? Any other pros and cons?
source share