枚举类转集合List
Posted 做猪呢,最重要的是开森啦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了枚举类转集合List相关的知识,希望对你有一定的参考价值。
1. 工具类:
org.apache.commons.lang3.EnumUtils;
2. 举个栗子:
2.1. 枚举类:
@Getter
@AllArgsConstructor
public enum RefundSortEnum
RETURN_ORDER_DATE(1, "returnOrderDate"),
RETURNABLE_TOTAL(2, "returnableTotal"),
RETURN_TOTAL(3, "returnTotal"),
REFUNDED_AMOUNT(4, "refundedAmount"),
REFUNDABLE_AMOUNT(5, "refundableAmount"),
REFUND_TOTAL_AMOUNT(6, "refundTotalAmount");
private Integer code;
private String value;
2.2. 转换:
EnumUtils.getEnumList
返回的结果是枚举类的集合,- 如需要属性集合,可以通过lambda再进行转化,也很简洁
public static void main(String[] args)
Stopwatch stopwatch = Stopwatch.createStarted();
List<String> nameList = EnumUtils.getEnumList(RefundSortEnum.class).stream().map(m -> m.name()).collect(Collectors.toList());
System.out.println(nameList);
List<String> valueList = EnumUtils.getEnumList(RefundSortEnum.class).stream().map(m -> m.getValue()).collect(Collectors.toList());
System.out.println(valueList);
List<String> flierList = EnumUtils.getEnumList(RefundSortEnum.class).stream().filter(v -> v.getCode() > 3).map(m -> m.name()).collect(Collectors.toList());
System.out.println(flierList);
// 判断某个枚举是否在枚举类中
System.out.println(EnumUtils.isValidEnum(RefundSortEnum.class, "RETURN_TOTAL"));
System.out.println(stopwatch.elapsed(TimeUnit.MILLISECONDS));
2.3. 结果:
[RETURN_ORDER_DATE, RETURNABLE_TOTAL, RETURN_TOTAL, REFUNDED_AMOUNT, REFUNDABLE_AMOUNT, REFUND_TOTAL_AMOUNT]
[returnOrderDate, returnableTotal, returnTotal, refundedAmount, refundableAmount, refundTotalAmount]
[REFUNDED_AMOUNT, REFUNDABLE_AMOUNT, REFUND_TOTAL_AMOUNT]
true
48
以上是关于枚举类转集合List的主要内容,如果未能解决你的问题,请参考以下文章