枚舉工具類java
getEnum(Class<E> enumClass, String enumName) 經過類返回一個枚舉,可能返回空工具
getEnumList(Class<E> enumClass) 經過類返回一個枚舉集合測試
getEnumMap(Class<E> enumClass) 經過類返回一個枚舉mapcode
isValidEnum(Class<E> enumClass, String enumName) 驗證enumName是否在枚舉中,返回true falseget
democlass
枚舉類 public enum ImagesTypeEnum { JPG,JPEG,PNG,GIF; } 測試 ImagesTypeEnum imagesTypeEnum = EnumUtils.getEnum(ImagesTypeEnum.class, "JPG"); System.out.println("imagesTypeEnum = " + imagesTypeEnum); System.out.println("--------------"); List<ImagesTypeEnum> imagesTypeEnumList = EnumUtils.getEnumList(ImagesTypeEnum.class); imagesTypeEnumList.stream().forEach( imagesTypeEnum1 -> System.out.println("imagesTypeEnum1 = " + imagesTypeEnum1) ); System.out.println("--------------"); Map<String, ImagesTypeEnum> imagesTypeEnumMap = EnumUtils.getEnumMap(ImagesTypeEnum.class); imagesTypeEnumMap.forEach((k, v) -> System.out.println("key:" + k + ",value:" + v)); System.out.println("-------------"); boolean result = EnumUtils.isValidEnum(ImagesTypeEnum.class, "JPG"); System.out.println("result = " + result); boolean result1 = EnumUtils.isValidEnum(ImagesTypeEnum.class, null); System.out.println("result1 = " + result1);
測試結果stream
imagesTypeEnum = JPG -------------- imagesTypeEnum1 = JPG imagesTypeEnum1 = JPEG imagesTypeEnum1 = PNG imagesTypeEnum1 = GIF -------------- key:JPG,value:JPG key:JPEG,value:JPEG key:PNG,value:PNG key:GIF,value:GIF ------------- result = true result1 = false