Setting an Enum value based on incoming String

前端 未结 4 945
慢半拍i
慢半拍i 2021-02-08 06:10

I have a number of setter methods which take an enum. These are based on incoming objects attribute. Rather than write a bunch of these is there a way around having to hard code

4条回答
  •  猫巷女王i
    2021-02-08 06:47

    I ended up using a simple object map:

    private static HashMap sideMap = new HashMap(7);
    static{
        sideMap.put("B", Side.BUY);
        sideMap.put("S", Side.SELL);
    }
    

    and simply using

    Obj.setSide(sideMap.get(zasAlloc.getM_buySellCode()));
    

提交回复
热议问题