【枚举工具类】EnumUtils

Hutool的EnumUtil EnumUtil是Hutool库中一个专门用于处理枚举类型的工具类。它为Java开发者提供了丰富的功能,以简化对未知枚举类型的操作。以下是EnumUtil获取枚举名称列表:通过方法,可以获取指定枚举类中所有枚举对象的名称列表,便于遍历、显示或记录枚举信息。获取枚举字段值:使用方法,可以获取指定枚举类中每个枚举对象的特定字段值,便于根据字段进行操作或展示。根据条件获取枚举对象:通过方法,可以基于lambda表达式和值,灵活地获取与之匹配的枚举对象,适用于需要通过属性值查找枚举对象的场景。 阅读详情

   

EnumUtils 介绍

一、官方API地址

官网地址:EnumUtils.html

二、EnumUtils 常用API

 1.导入POM文件
   这里使用的是:3.9 的版本,还是比较新的

  1. <dependency>

  2.     <groupId>org.apache.commons</groupId>

  3.     <artifactId>commons-lang3</artifactId>

  4.      <version>3.9</version>

  5. </dependency>

 2.常用API介绍

   API介绍参照下方实例

 3.API的使用

  1. enum ImagesTypeEnum {

  2. JPG, JPEG, PNG, GIF;

  3. }

   getEnum(Class<E> enumClass, String enumName) 通过类返回一个枚举,可能返回空

ImagesTypeEnum imagesTypeEnum = EnumUtils.getEnum(ImagesTypeEnum.class, "JPG");

 

   getEnumIgnoreCase(Class<E> enumClass, String enumName) 

   通过类返回一个枚举,enumName忽略大小写,可能返回空

ImagesTypeEnum imagesTypeEnum = EnumUtils.getEnum(ImagesTypeEnum.class, "JPG");

 

   getEnumList(Class<E> enumClass) 通过类返回一个枚举集合

  1. List<ImagesTypeEnum> imagesTypeEnumList = EnumUtils.getEnumList(ImagesTypeEnum.class);

  2. imagesTypeEnumList.stream().forEach(

  3. imagesTypeEnum1 -> System.out.println("imagesTypeEnum1 = " + imagesTypeEnum1)

  4. );

  5. // imagesTypeEnum1 = JPG

   getEnumMap(Class<E> enumClass) 通过类返回一个枚举map

  1. Map<String, ImagesTypeEnum> imagesTypeEnumMap = EnumUtils.getEnumMap(ImagesTypeEnum.class);

  2. imagesTypeEnumMap.forEach((k, v) -> System.out.println("key:" + k + ",value:" + v));

  3. // key:JPG,value:JPG

  4. // key...

   isValidEnum(Class<E> enumClass, String enumName) 验证enumName是否在枚举中,返回true false

  1. boolean result = EnumUtils.isValidEnum(ImagesTypeEnum.class, "JPG");

  2. System.out.println("result = " + result); // result = true

  3. boolean result1 = EnumUtils.isValidEnum(ImagesTypeEnum.class, null);

  4. System.out.println("result1 = " + result1); // result1 = false

 

   isValidEnumIgnoreCase(Class<E> enumClass, String enumName) 

   验证enumName是否在枚举中,返回true false,enumName忽略大小写

private static String[] getEnvArr(){
        List enumList = EnumUtils.getEnumList(EnvEnum.class).stream().map(envEnum->envEnum.getProfile()).filter(env->!env.equals("prod")).collect(Collectors.toList());
        String[] envInitArr = new String[enumList.size()];
        String[] envArr = (String[]) enumList.toArray(envInitArr);
        return envArr;
    }

springboot的环境@Profile注解的value必须是初始化的数组,不能使用Enmu赋值,也不能使用方法将枚举的value转成数组,包括扩静态方法,暂时没有更好的方法

package com.pactera.data.governance.enums;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.EnumUtils;

public enum EnvEnum {
    DEV("dev"),
    TEST("test"),
    TRN("trn"),
    UAT("uat"),
    PROD("prod");
    private String profile;
    
    private EnvEnum(String profile) {
        this.profile=profile;
    }

    public String getProfile() {
        return profile;
    }

    public void setProfile(String profile) {
        this.profile = profile;
    }
    public static String[] getEnvArr(){
        List enumList = EnumUtils.getEnumList(EnvEnum.class).stream().map(envEnum->envEnum.getProfile()).filter(env->!env.equals("prod")).collect(Collectors.toList());
        String[] envInitArr = new String[enumList.size()];
        String[] envArr = (String[]) enumList.toArray(envInitArr);
        return envArr;
    }    
}

 

EnumUtil 枚举工具类 Java项目中用的工具类 阅读详情

相关推荐

Java 基础学习总结(210)—— Apache Commons EnumUtils:让枚举操作从繁琐到优雅的蜕变

文章摘要:Apache Commons Lang3的EnumUtils工具类为Java枚举操作提供高效解决方案,可减少70%的代码量。它支持安全获取枚举(名称/索引)、合法性校验、集合化处理、自定义字段操作和序列化辅助等五大功能。相比原生JDK实现,EnumUtils避免了手写循环和异常处理,使代码更简洁可靠。文章通过电商订单状态管理案例,展示了如何用EnumUtils优化枚举转换、校验和展示逻辑,并给出性能优化建议(缓存枚举列表、使用映射查询)和常见误区提示(线程安全、方法选择)。该工具特别适合处理外部参

科技D人生 313

枚举工具类 EnumUtils.java

简介 EnumUtils工具类, 用于读取枚举中的code和value值。 使用有限制, 枚举类必须提供 getCode 和 getValue 两个方法,例如NODEFINE("未定义", "未定义"); api 返回枚举类中的所有值readEnum(Class<?> classes); 根据输入的code, 取出对应的 value值getValue(Class<?> classes, Object code); 根据输入的value, 取出对应的 code值g...

lud 2930

org.apache.commons.lang jar包下载(commons-lang3-3.1.jar)

commons-lang3.3.1.jar、Apache Commons包中的一个,包含了一些数据类型工具类,是java.lang.*的扩展。必须使用的jar包。为JRE5.0+的更好的版本所提供 Jar文件包含的类: META-INF/MANIFEST.MFMETA-INF/LICENSE.txtMETA-INF/NOTICE.txtorg.apache.commons.lang.ArrayUtils.class org.apache.commons.lang.BitField.class org.apache.commons.lang.BooleanUtils.class org.apache.commons.lang.CharEncoding.class org.apache.commons.lang.CharRange.class org.apache.commons.lang.CharSet.class org.apache.commons.lang.CharSetUtils.class org.apache.commons.lang.CharUtils.class org.apache.commons.lang.ClassUtils.class org.apache.commons.lang.Entities$ArrayEntityMap.class org.apache.commons.lang.Entities$BinaryEntityMap.class org.apache.commons.lang.Entities$EntityMap.class org.apache.commons.lang.Entities$HashEntityMap.class org.apache.commons.lang.Entities$LookupEntityMap.class org.apache.commons.lang.Entities$MapIntMap.class org.apache.commons.lang.Entities$PrimitiveEntityMap.class org.apache.commons.lang.Entities$TreeEntityMap.class org.apache.commons.lang.Entities.class org.apache.commons.lang.IllegalClassException.class org.apache.commons.lang.IncompleteArgumentException.class org.apache.commons.lang.IntHashMap$Entry.class org.apache.commons.lang.IntHashMap.class org.apache.commons.lang.LocaleUtils.class org.apache.commons.lang.NotImplementedException.class org.apache.commons.lang.NullArgumentException.class org.apache.commons.lang.NumberRange.class org.apache.commons.lang.NumberUtils.class org.apache.commons.lang.ObjectUtils$Null.class org.apache.commons.lang.ObjectUtils.class org.apache.commons.lang.RandomStringUtils.class org.apache.commons.lang.SerializationException.class org.apache.commons.lang.SerializationUtils.class org.apache.commons.lang.StringEscapeUtils.class org.apache.commons.lang.StringUtils.class org.apache.commons.lang.SystemUtils.class org.apache.commons.lang.UnhandledException.class org.apache.commons.lang.Validate.class org.apache.commons.lang.WordUtils.class org.apache.commons.lang.builder.CompareToBuilder.class org.apache.commons.lang.builder.EqualsBuilder.class org.apache.commons.lang.builder.HashCodeBuilder.class org.apache.commons.lang.builder.ReflectionToStringBuilder$1.class org.apache.commons.lang.builder.ReflectionToStringBuilder.class org.apache.commons.lang.builder.StandardToStringStyle.class org.apache.commons.lang.builder.ToStringBuilder.class org.apache.commons.lang.builder.ToStringStyle$DefaultToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$MultiLineToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$NoFieldNameToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$ShortPrefixToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$SimpleToStringStyle.class org.apache.commons.lang.builder.ToStringStyle.class org.apache.commons.lang.enum.Enum$Entry.class org.apache.commons.lang.enum.Enum.class org.apache.commons.lang.enum.EnumUtils.class org.apache.commons.lang.enum.ValuedEnum.class org.apache.commons.lang.enums.Enum$Entry.class org.apache.commons.lang.enums.Enum.class org.apache.commons.lang.enums.EnumUtils.class org.apache.commons.lang.enums.ValuedEnum.class org.apache.commons.lang.exception.ExceptionUtils.class org.apache.commons.lang.exception.Nestable.class org.apache.commons.lang.exception.NestableDelegate.class org.apache.commons.lang.exception.NestableError.class org.apache.commons.lang.exception.NestableException.class org.apache.commons.lang.exception.NestableRuntimeException.class org.apache.commons.lang.math.DoubleRange.class org.apache.commons.lang.math.FloatRange.class org.apache.commons.lang.math.Fraction.class org.apache.commons.lang.math.IntRange.class org.apache.commons.lang.math.JVMRandom.class org.apache.commons.lang.math.LongRange.class org.apache.commons.lang.math.NumberRange.class org.apache.commons.lang.math.NumberUtils.class org.apache.commons.lang.math.RandomUtils.class org.apache.commons.lang.math.Range.class org.apache.commons.lang.mutable.Mutable.class org.apache.commons.lang.mutable.MutableBoolean.class org.apache.commons.lang.mutable.MutableByte.class org.apache.commons.lang.mutable.MutableDouble.class org.apache.commons.lang.mutable.MutableFloat.class org.apache.commons.lang.mutable.MutableInt.class org.apache.commons.lang.mutable.MutableLong.class org.apache.commons.lang.mutable.MutableObject.class org.apache.commons.lang.mutable.MutableShort.class org.apache.commons.lang.text.CompositeFormat.class org.apache.commons.lang.text.StrBuilder$StrBuilderReader.class org.apache.commons.lang.text.StrBuilder$StrBuilderTokenizer.class org.apache.commons.lang.text.StrBuilder$StrBuilderWriter.class org.apache.commons.lang.text.StrBuilder.class org.apache.commons.lang.text.StrLookup$MapStrLookup.class org.apache.commons.lang.text.StrLookup.class org.apache.commons.lang.text.StrMatcher$CharMatcher.class org.apache.commons.lang.text.StrMatcher$CharSetMatcher.class org.apache.commons.lang.text.StrMatcher$NoMatcher.class org.apache.commons.lang.text.StrMatcher$StringMatcher.class org.apache.commons.lang.text.StrMatcher$TrimMatcher.class org.apache.commons.lang.text.StrMatcher.class org.apache.commons.lang.text.StrSubstitutor.class org.apache.commons.lang.text.StrTokenizer.class org.apache.commons.lang.time.DateFormatUtils.class org.apache.commons.lang.time.DateUtils$DateIterator.class org.apache.commons.lang.time.DateUtils.class org.apache.commons.lang.time.DurationFormatUtils$Token.class org.apache.commons.lang.time.DurationFormatUtils.class org.apache.commons.lang.time.FastDateFormat$CharacterLiteral.class org.apache.commons.lang.time.FastDateFormat$NumberRule.class org.apache.commons.lang.time.FastDateFormat$PaddedNumberField.class org.apache.commons.lang.time.FastDateFormat$Pair.class org.apache.commons.lang.time.FastDateFormat$Rule.class org.apache.commons.lang.time.FastDateFormat$StringLiteral.class org.apache.commons.lang.time.FastDateFormat$TextField.class org.apache.commons.lang.time.FastDateFormat$TimeZoneDisplayKey.class org.apache.commons.lang.time.FastDateFormat$TimeZoneNameRule.class org.apache.commons.lang.time.FastDateFormat$TimeZoneNumberRule.class org.apache.commons.lang.time.FastDateFormat$TwelveHourField.class org.apache.commons.lang.time.FastDateFormat$TwentyFourHourField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitMonthField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitNumberField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitYearField.class org.apache.commons.lang.time.FastDateFormat$UnpaddedMonthField.class org.apache.commons.lang.time.FastDateFormat$UnpaddedNumberField.class org.apache.commons.lang.time.FastDateFormat.class org.apache.commons.lang.time.StopWatch.class

EnumUtils 介绍

EnumUtils 介绍 一、官方API地址 官网地址:EnumUtils.html 二、EnumUtils 常用API 1.导入POM文件 这里使用的是:3.9 的版本,还是比较新的 <dependency> <groupId>org.apache.commons</groupId> &...

工作中遇到bug 2943

基于STM32的多参数水质检测仪系统 | 保姆式教程 开源可复刻

本项目基于STM32F103C8T6主控,搭配pH、TDS、浊度、水温四大核心水质检测模块,完整实现了多参数水质检测仪的全流程开发,从多通道ADC采集、数字滤波、温度补偿、专业校准、阈值报警到人机交互,全流程无跳步保姆式拆解,配套逐行注释可直接编译的开源代码,新手跟着步骤一步步操作,就能顺利完成项目复刻

ZhongWeiXiang9的博客 484

波分复用-Zblock仿真

Z-BLOCK是一种用于光通信模块的光学元件,其核心结构是在玻璃块一侧贴有4个特定波长滤光片,另一侧镀有反射膜和增透膜。光信号在内部呈现Z字形反射路径,实现波长分离。文章以可见光LED为例,在ZEMAX中演示了非序列模式下的建模过程,重点介绍了滤光片和膜层的参数设置方法。需要注意的是,入射角度需根据玻璃折射率、通道高度和厚度精确计算,否则可能导致光泄漏。该建模方法同样适用于光通信中的光纤激光器系统。

Zhaxibamao的博客 1743

根据值获取枚举类对象工具类EnumUtils

一,背景: 项目中枚举用的比较多,使用枚举时经常要根据枚举某个属性获取枚举对象,这就导致了我们每个枚举类里都要定义一个获取的方法,例如 import lombok.AllArgsConstructor; import lombok.Getter; /** * @author kismet * @version 1.0 * @since 2019/8/30 */ @Getter @AllA...

kismet2399的博客 1919

RK356X linux系统移植RTL8723BU模块遇到的坑

RK356X linux系统移植RTL8723BU模块遇到的坑

目前从事linux驱动开发,多交流多学习吧 954

Apache EnumUtils枚举工具类

/ 枚举常量定义CREATED("已创建"),PAID("已支付"),SHIPPED("已发货"),DELIVERED("已送达"),CANCELLED("已取消");// 枚举属性// 构造方法。

zhangyifang_009的博客 415

基于深度学习的航拍图像中地物变化检测

随着无人机技术的飞速发展,航拍图像成为获取地表信息的重要手段之一。然而,由于地球表面的不断变化,监测和识别航拍图像中的地物变化成为一项具有挑战性的任务。在人工智能领域,深度学习技术的兴起为地物变化检测提供了全新的解决方案。本文将介绍基于深度学习的航拍图像中地物变化检测的方法,并附上相应的代码实例。

一键难忘的博客 4175

6-2 GPU GART 绑定机制与 TLB 失效

本文详细解析了AMD GPU的GART(Graphics Address Remapping Table)绑定机制实现。主要内容包括: 绑定流程:从ttm_tt_bind()到硬件PTE更新的完整调用链,重点分析amdgpu_gart_bind()函数实现。 关键函数: amdgpu_gart_bind():执行安全检查、记录映射关系、更新页表等操作 amdgpu_gart_map():将CPU页映射为多个GPU页并填充PTE amdgpu_gmc_set_pte_pde():处理硬件特定的PTE格式 内存

图形/计算GPU的驱动与虚拟化技术分享园地 2646

java enumutils_关于apache的commons-lang3包中的EnumUtils枚举工具类对枚举类类型转换、EnumSet类型处理等操作...

一、前言关于apache的commons-lang3(3.3.2)源码包中的org.apache.commons.lang3.EnumUtils枚举工具类,对枚举值转换为LinkedHashMap的Map、枚举值转换为List集合序列、获取枚举值为EnumSet集合等常用的操作,详情参见如下源码说明。二、源码说明packageorg.apache.commons.lang3;@b@@b@impo...

weixin_32815027的博客 761

零基础做完YOLOv8船只检测系统:HRSC2016数据集 + 完整UI,稳过答辩

亲爱的同学,通过这篇教程,你已经掌握了“基于YOLOv8的HRSC2016微小船只检测系统”从数据集处理、模型训练到Tkinter界面开发的全流程。这个课题既贴合“海事智能化”的产业战略,又涵盖了“目标检测、深度学习、界面开发”等多个毕设评分点,非常适合作为计算机或海事工程专业的毕业设计。在实践中,你可能会遇到“海面背景复杂导致船只漏检”“微小船只识别精度不足”“UI与模型推理同步卡顿”等挑战——别担心,这些都是技术成长的必经之路。

FJN110的博客 108

自定义枚举工具类 EnumUtils.java

【代码】自定义枚举工具类 EnumUtils.java。

IT枫斗者的博客 657

SAP 打印输出中 `#` 字符替代日文汉字的根本原因与全面解决方案

只要认清设备类型 / 字符集 / 字体三角关系,并合理利用PDFUCSWINCF或 UPE + URDT,LP01里那些恼人的就会彻底消失,日文汉字将在纸面与 PDF 中如预览一样完美呈现。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 813

新手福利!大龙虾OpenClaw可视化管理工具来了!告别命令行,这个可视化工具让OpenClaw管理变得超简单

还在用命令行管理OpenClaw?🚀 这个跨平台可视化工具支持14+AI模型,一键切换,还能对接微信飞书钉钉等8大消息渠道,让AI助手管理变得简单直观 ✅ 暗色主题护眼舒适,Rust后端高性能低占用,新手也能轻松上手 🎯

XiaoqiangClub的博客 760

java枚举工具类_枚举工具类 EnumUtils

/** Copyright 2014-2018 xfami.cn. All rights reserved.* Support:https://www.xfami.cn*/packagecom.cymc.page.common.enumobj;importjava.lang.reflect.Field;importjava.lang.reflect.InvocationTargetExceptio...

weixin_36391538的博客 879

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

EnumUtils 枚举工具类

public class EnumUtils { public static <T, V, S> S getEnumProperty(T t, Function<T, V> function1, Function<V, S> function2) { return getEnumProperty(t, function1, null, function2); } public static <T, V, S> S ge

weixin_43935720的博客 250

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

枚举工具类 EnumUtils

这是一个枚举工具类,有时候一个枚举自身为空,我们程序在调用他的方法时(如:name(),ordinal()),就会报空指针异常;下面写这个工具类就是为了来避免这些问题的,有些外部引用的没有贴出来,不过都是些挺简单的工具代码; 代码如下: /* * Copyright 2014-2018 xfami.cn. All rights reserved. * Support: https:...

weixin_30485799的博客 481

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

java enumutils_commons-lang3:EnumUtils

枚举工具类getEnum(Class enumClass, String enumName)通过类返回一个枚举,可能返回空getEnumList(Class enumClass)通过类返回一个枚举集合getEnumMap(Class enumClass)通过类返回一个枚举mapisValidEnum(Class enumClass, String enumName)验证enumName是否...

weixin_35987371的博客 427

EnumUtils重构及自定义@EnumValue注解自动校验枚举值

EnumUtils主要功能介绍 该简单介绍可看笔者之前文章 java枚举工具类(减少每个枚举中重复的方法) 但以上该方式存在一些问题,例如枚举值(value)可能为String也可能为Byte等等,不止只是Integer类型,而且有些枚举可能不需要枚举名称(name)属性,只需要个枚举值value属性即可,以上最简单的实现方式就不支持这些需求了,故通过拆分BaseEnum接口以及增加泛型来支持该要...

mayfly_hml的博客 3946

java枚举工具类_java枚举工具类(减少自定义枚举中重复的方法)

前言几乎每个系统或多或少都会有些枚举,用来减少硬编码。有时保存数据的时候需要使用下拉框选择枚举,如下图枚举值 1->GET, 2->POST, 3->PUT, 4->DELETE,而传到后端的是value比如选择GET时,传到后端保存的是1这个值,controller接收的就是Integer类型。 如上图,后端可定义枚举类MethodEnum/*** @author mei...

weixin_35227213的博客 803

枚举工具类使用方法

String com.comtop.lcam.common.util.EnumUtils.getValue(IEnum[] ie, String key) EnumUtils.getValue(ProvinceEnumForIPB.values(), form.getProvinceId());  EnumUtils.getValue(枚举类名称.values(),String key

渡渡鸟的博客 1032

枚举工具类

java枚举工具类

qq_43049583的博客 756
上一篇: pgAdmin 4如何以客户端形式打开不能使用IE浏览器
下一篇: springboot项目中访问druid内置监控页面
qq_34412985
博客等级 码龄11年 143粉丝 983原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值