private static Map<String, Consumer<ProductVO>> maps = new HashMap<String, Consumer<ProductVO>>(); static { //這裏用到landa表達式,新特性。 其中 Cat,Dog 能夠當作 if-else 中的條件 maps.put("type1", productVO -> test1(productVO)); maps.put("type2", productVO -> test2(productVO)); } public static void main(String[] args) { Consumer<ProductVO> cat = maps.get("Cat"); ProductVO productVO = new ProductVO(); productVO.setBrandName("sasdfs"); cat.accept(productVO); } private static Boolean test1(ProductVO productVO) { System.out.println("RunCase2" + productVO.getBrandName()); return true; } private static Boolean test2(ProductVO productVO) { System.out.println("RunCase1" + productVO.getBrandName()); return false; }