Still using Java enumeration: Understanding enumeration examples. Slightly modified.
package mark.demo;public class EnumDemo {public static void main(String[] args) {for (Color color : Color.values()) {System.out.println(color);}}public enum Color {RED("red color", 0), GREEN("green color", 1), BLUE("blue color", 2), YELLOW("yellow color", 3);Color(String name, int id) {_name = name;_id = id;}private String _name;private int _id;public String getName() {return _name;}public int getId() {return _id;}}}
Print results
I thought it would print the following format
However, this is not the case. For details, see the source code of Enum and the tostring method:
/** * Returns the name of this enum constant, as contained in the * declaration. This method may be overridden, though it typically * isn't necessary or desirable. An enum type should override this * method when a more "programmer-friendly" string form exists. * * @return the name of this enum constant */ public String toString() {return name; }
Note: The name of Enum constant (name of the enumerated Constant Value) is returned ).
If we want to get the expected results, we need to rewrite the tostring method.
package mark.demo;public class EnumDemo {public static void main(String[] args) {for (Color color : Color.values()) {System.out.println(color);}}public enum Color {RED("red color", 0), GREEN("green color", 1), BLUE("blue color", 2), YELLOW("yellow color", 3);Color(String name, int id) {_name = name;_id = id;}private String _name;private int _id;public String getName() {return _name;}public int getId() {return _id;}@Overridepublic String toString() {return _name + "," + _id;}}}
In the code above, the enhanced for loop is used to call the values () method to traverse the enumerated values. You can also traverse the values as follows:
for (Color color : Color.values()) {System.out.println(color.getName() + "," + color.getId());}
Equals and hashcode methods of the enum class
/** * Returns true if the specified object is equal to this * enum constant. * * @param other the object to be compared for equality with this object. * @return true if the specified object is equal to this * enum constant. */ public final boolean equals(Object other) { return this==other; } /** * Returns a hash code for this enum constant. * * @return a hash code for this enum constant. */ public final int hashCode() { return super.hashCode(); }
They are all final. Obviously, designers do not want subclass to rewrite these two methods.
Indeed, the two methods cannot be rewritten in enumeration. But how to use this method?
Now, we will not rewrite the tostring method to see the equals effect.
public enum Color {RED("red color", 0), GREEN("green color", 1), BLUE("blue color", 2), YELLOW("yellow color", 3);Color(String name, int id) {_name = name;_id = id;}private String _name;private int _id;public String getName() {return _name;}public int getId() {return _id;}}
Test results:
Public static void main (string [] ARGs) {system. out. println (color. red. equals (""); // falsesystem. out. println (color. red. equals ("red color"); // falsesystem. out. println (color. red. equals ("red"); // falsesystem. out. println (color. red. equals (color. blue); // false // the same object system. out. println (color. red. equals (color. red); // true // actually two stringsystems. out. println (color. red. tostring (). equals ("red"); // true // actually two stringsystems. out. println (color. red. getname (). equals ("red color"); // true // hashcodesystem. out. println ("color. red. hashcode = "+ color. red. hashcode ());}
If you overwrite the tostring method
System.out.println(Color.RED.toString().equals("RED"));
The returned value may be true or false, depending on your tostring implementation.
As shown in the following implementation method: return the capital form of _ name.
@Overridepublic String toString() {return this._name.toUpperCase();}
To sum up, we recommend that you rewrite the tostring method as needed and make good use of the equals method.