有时候,有一些下拉
选择器
的数据项,我们采取了枚举的方式简单记录,但是里面的东西多,前端不想写死,需要提供接口返回。
示例:
枚举
/**
* @Author: JCccc
* @Description:
* @Date: Create in 10:51 2021/1/25
public enum EducateStatusEnum {
* 学生 学业状态
IN_STUDYING((short) 1, "在读"),
UNDERGRADUATE((short) 2, "肆业"),
SUSPENDED((short) 3, "休学"),
DROPPED((short) 4, "辍学"),
QUITED((short) 5, "退学"),
GRADUATED((short) 6, "已毕业"),
public short code;
public String name;
EducateStatusEnum(Short code, String name) {
this.code = code;
this.name = name;
public int getCode() {
return this.code;
public String getName() {
return this.name;
public static EducateStatusEnum findEnumByCode(Integer code) {
for (EducateStatusEnum statusEnum : EducateStatusEnum.values()) {
if (statusEnum.getCode() == code) {
return statusEnum;
throw new IllegalArgumentException("code is not support");
public static EducateStatusEnum findEnumByName(String name) {
for (EducateStatusEnum statusEnum : EducateStatusEnum.values()) {
if (statusEnum.getName().equals(name)) {
return statusEnum;
throw new IllegalArgumentException("name is not support");
}
工具类:
ps :如果不想引入jar,自己写对应的判空逻辑即可。
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
/**
* @Author: JCccc
* @Description: 将枚举转换为list类型
* @Date: Create in 19:03 2021/1/26
public class EnumListUtil {
private static String ENUM_CLASSPATH="java.lang.Enum";
public static List<Map<String, Object>> enumToListMap(Class<?> enumClass) {
List<Map<String, Object>> resultList= new ArrayList<>();
if (!ENUM_CLASSPATH.equals(enumClass.getSuperclass().getCanonicalName())) {
return resultList;
// 获取所有public方法
Method[] methods = enumClass.getMethods();
List<Field> fieldList = new ArrayList<>();
//1.通过get方法提取字段,避免get作为自定义方法的开头,建议使用 ‘find’或其余命名
Arrays.stream(methods)
.map(Method::getName)
.filter(
methodName -> methodName.startsWith("get") && !"getDeclaringClass".equals(methodName) && !"getClass".equals(methodName)
).forEachOrdered(methodName -> {
try {
Field field = enumClass.getDeclaredField(StringUtils.uncapitalize(methodName.substring(3)));
if (null != field) {
fieldList.add(field);
} catch (NoSuchFieldException | SecurityException e) {
e.printStackTrace();
//2.将字段作为key,逐一把枚举值作为value 存入list
if (CollectionUtils.isEmpty(fieldList)) {
return resultList;
Enum<?>[] enums = (Enum[]) enumClass.getEnumConstants();
for (Enum<?> anEnum : enums) {
Map<String, Object> map = new HashMap<>(fieldList.size());
for (Field field : fieldList) {
field.setAccessible(true);
try {
// 向map集合添加字段名称 和 字段值
map.put(field.getName(), field.get(anEnum));
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
// 将Map添加到集合中
resultList.add(map);
return resultList;
public static void main(String[] args) {
// 枚举类
System.out.println(enumToListMap(EducateStatusEnum.class));
}
实现效果:
{name=在读, code=1},
{name=肆业, code=2},
{name=休学, code=3},
{name=辍学, code=4},
{name=退学, code=5},
{name=已毕业, code=6}
给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。(Java语言实现)