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
|