根据值获取枚举类对象工具类EnumUtils
枚举类
public enum Sex {
man("M","男"),woman("W","女");
private String myCode;
private String value;
private Sex(String myCode, String value) {
this.myCode = myCode;
this.value = value;
}
public String getMyCode() {
return myCode;
}
public void setMyCode(String myCode) {
this.myCode = myCode;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
公共工具类
/**
* https://blog.csdn.net/u010125873/article/details/81386320
* @author admin
*/
public class EnumUtils { private static Map<Class,Object> map = new ConcurrentHashMap<>(); /**
* 根据条件获取枚举对象
* @param className 枚举类
* @param predicate 筛选条件
* @param <T>
* @return
*/
@SuppressWarnings("unchecked")
public static <T> Optional<T> getEnumObject(Class<T> className, Predicate<T> predicate) {
if(!className.isEnum()){
// logger.info("Class 不是枚举类");
return null;
}
Object obj = map.get(className);
T[] ts = null;
if(obj == null){
ts = className.getEnumConstants();
map.put(className,ts);
}else{
ts = (T[])obj;
}
return Arrays.stream(ts).filter(predicate).findAny();
} public static void main(String[] args) { Optional<Sex> m = EnumUtils.getEnumObject(Sex.class, e -> e.getMyCode().equals("W"));
System.out.println(m.isPresent() ? m.get().getValue():null);
}
}
特点:
1)能自动获取枚举类中的方法,如e.getMyCode()和m.get().getValue()
参考:https://blog.csdn.net/u010125873/article/details/81386320
另外一种通过反射实现:
public class EnumHelperUtil{
/**
* 使用枚举类型对应的typeName获取枚举类型
* @param clazz 枚举类的class
* @param getTypeNameMethodName 传入的typeName的get方法
* @param typeName 传入的typeName值,这个方法为Object类型
*/
public static <T extends Enum<T>> T getEnumsValue(Class<T> clazz,String getTypeNameMethodName, Object typeName){
T result = null;
try{
T[] arr = clazz.getEnumConstants();
Method targetMethod = clazz.getDeclaredMethod(getTypeNameMethodName);
Object typeNameVal = null;
for(T entity:arr){
typeNameVal = targetMethod.invoke(entity);
if(typeNameVal.equals(typeName)){
result = entity;
break;
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
Sex byStringTypeName = EnumHelperUtil.getEnumsValue(Sex.class, "getMyCode", 1);
System.out.println(byStringTypeName.getValue());
}
}
链接:https://blog.csdn.net/busbanana/article/details/72954676
注意:原文中提供了Integer和String的2种方法,而本例统一转为Object,只提供一个方法。
根据值获取枚举类对象工具类EnumUtils的更多相关文章
- 第三章 EnumUtil根据值获取枚举对象
项目中使用枚举类的好处这里不再赘述,在使用枚举值时,通常需要根据值来获取枚举对象,下面介绍两种实现方案: 1.在枚举类中定义方法实现 首先给出如下性别枚举类: public enum SexEnum ...
- 对象工具类 - ObjectUtils.java
对象工具类,提供对象克隆.获取对象属性.类型判断.Map转换对象.对象转Map.设置对象属性等. 源码如下:(点击下载 - ObjectUtils.java .JsonUtils.java .gso ...
- EncryptHelper加密对象-工具类
using System; using System.IO; using System.Security.Cryptography; using System.Text; using System.W ...
- Spring MVC普通类或工具类中调用service报空空指针的解决办法(调用service报java.lang.NullPointerException)
当我们在非Controller类中应用service的方法是会报空指针,如图: 这是因为Spring MVC普通类或工具类中调用service报空null的解决办法(调用service报java.la ...
- Java容器类Collection,List,Set,Map.,Iterator,Collections工具类,Arrays工具类,Comparable
Java容器类Collection,List,Set,Map.,Iterator,Collections工具类,Arrays工具类,Comparable接口,泛型 Collection,List,Se ...
- C++类对象作为类成员
//当其他类的对象作为本类的成员 构造的时候先去构造类的对象 再去构造自身 //析构函数: 先进后出 先去构造Phone 再去构造Person Person先退出 Phone后退出 //析构与构造顺序 ...
- JAVA 获取文件的MD5值大小以及常见的工具类
/** * 获取文件的MD5值大小 * * @param file * 文件对象 * @return */ public static String getMD5(File file) { FileI ...
- 获取Spring容器Bean对象工具类
在开发中,总是能碰到用注解注入不了Spring容器里面bean对象的问题.为了解决这个问题,我们需要一个工具类来直接获取Spring容器中的bean.因此就写了这个工具类,在此记录一下,方便后续查阅. ...
- 获取SpringBean对象工具类
获取SpringBean工具类 SpringBeanUtil代码如下: package com.meeno.wzq.util; import org.springframework.beans.Bea ...
随机推荐
- android -------- 解决RecyclerView显示不全只显示一条item的问题
布局文件1 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android= ...
- mysql 5.6配置
简洁版: [client] port = 3306 socket = /weyeedata/mysql/run/mysql.sock [mysqld] innodb_buffer_pool_size ...
- [转]windows环境下启动与停止.jar文件
原文地址:https://www.jianshu.com/p/b12fc379d171 1 .启动 在xx.jar同级目录下建立run.bat文件,在run.bat文件中编辑下列文本. @echo o ...
- 备忘-VSCODE、apache配置
一个像素点的光标:https://files.cnblogs.com/files/zjfree/mouse.zip VSCODE配置备忘: { "editor.fontLigatures&q ...
- Linux中搜索大于200M的文件
Linux中清理磁盘空间时,经常需要找出大于200M的文件. 这个命令可以实现这个功能: find / -size +200M -exec du -h {} \;
- 【446】Deep Learning
ref: 深度学习基础介绍 机器学习19 神经网络NN算法 ref: 深度学习基础介绍 机器学习11 神经网络算法应用上 ref: 深度学习基础介绍 机器学习12 神经网络算法应用下 ref: 神经网 ...
- [LeetCode] 271. Encode and Decode Strings 加码解码字符串
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...
- [LeetCode] 401. Binary Watch 二进制表
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...
- array_merge_recursive 的使用
作用:递归合并数组 场景:在平时开发中,我们可能遇到获取的数据是多个维度的数据,而我们只需要其中的一个. 代码如下: $project_id = 'project_id'; $arr1 = [ 'pr ...
- 【LEETCODE】52、数组分类,简单级别,题目:717,661,746,628,643,849
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...