2

Java enum对象详细编写

 7 months ago
source link: https://cfonheart.github.io/2019/03/15/Java-enum%E5%AF%B9%E8%B1%A1%E8%AF%A6%E7%BB%86%E7%BC%96%E5%86%99/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

简单demo实现

下方是一份简单的java代码。一般我们最简单的enum写法会是如下即可

enum Fruit{
Apple, Banana;

实际上这些Apple, Banana相当于是一个无参数的构造对象。如下想要运行有参数的构造对象,需要自己如下定义对应的构造方法。我们保证里面存了一个name和一个id。

import java.util.HashMap;
enum StreamCeommandType {
UNSUPPORT_TYPE("UNSUPPORT TYPE", 0),
START_STREAMJOB("START STREAMJOB", 1),
STOP_STREAMJOB("STOP STREAMJOB", 2),
CREATE_STREAMJOB("CREATE STREAMJOB", 3),
DROP_STREAMJOB("DROP STREAMJOB", 4),
START_APP("START APP", 5),
STOP_APP("STOP APP", 6),
CREATE_APP("CREATE APP", 7),
DROP_APP("DROP APP", 8),
USE_APP("USE APP", 9),
LIST_STREAMJOBS("LIST STREAMJOBS", 10),
SHOW_APP("SHOW APP", 11)
private String name;
int id;
public static HashMap<String, StreamCeommandType> commands = new HashMap<String, StreamCeommandType>();
static {
commands.put("START STREAMJOB", START_STREAMJOB);
commands.put("STOP STREAMJOB", STOP_STREAMJOB);
commands.put("CREATE STREAMJOB", CREATE_STREAMJOB);
commands.put("DROP STREAMJOB", DROP_STREAMJOB);
commands.put("START APP", START_APP);
commands.put("STOP APP", STOP_APP);
commands.put("CREATE APP", CREATE_APP);
commands.put("DROP APP", DROP_APP);
commands.put("USE APP", USE_APP);
commands.put("LIST STREAMJOBS", LIST_STREAMJOBS);
commands.put("SHOW APP", SHOW_APP);
StreamCeommandType(String name, int id){
this.name = name;
this.id = id;
public int getValue(){
return id;
@Override
public String toString(){
return String.valueOf(id) + " : " + name;
public static StreamCeommandType find(String name){
if(name == null)
return UNSUPPORT_TYPE;
if(commands.containsKey(name))
return commands.get(name);
else return UNSUPPORT_TYPE;
public class StreamCommandParser {
public static void main(String [] args){
System.out.println(StreamCeommandType.find("LIST STREAMJOBS"));
System.out.println(StreamCeommandType.find("haha"));

下方是个scala版本的类似实现

object UnkownCommandType extends Enumeration{
type UnkownCommandType = Value
val START_STREAMJOB = Value("start streamjob")
val STOP_STREAMJOB = Value("stop streamjob")
val CREATE_STREAMJOB = Value("create streamjob")
val DROP_STREAMJOB = Value("drop streamjob")
val START_APP = Value("start app")
val STOP_APP = Value("stop app")
val CREATE_APP = Value("create app")
val DROP_APP = Value("drop app")
val USE_APP = Value("use app")
val LIST_JOBS = Value("list streamjobs")
val UNSUPPORT_TYPE = Value("unsupport type")
val arr$: Array[UnkownCommandType] = values.toArray
val len$ = arr$.length
// arr$.foreach(x => println(x.toString) )
lazy val commands: mutable.Map[String, UnkownCommandType] ={
var commands = mutable.HashMap[String, UnkownCommandType]()
arr$.foreach(x => commands.put(x.toString, x))
commands
def find(command: Array[String]): Option[UnkownCommandType] = {
if (null == command)
Some(UNSUPPORT_TYPE)
else{
val cmd: String = command.length match {
case 0 => ""
case 1 => command(0).toLowerCase
case _ => {
if(command(1) == "application") command(0).toLowerCase + " " + "app"
else (command(0) + " " + command(1)).toLowerCase
if (commands.contains(cmd)) commands.get(cmd)
else Some(UNSUPPORT_TYPE)
def == (unkownCommandType: UnkownCommandType): Boolean ={
toString == unkownCommandType.toString

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK