将枚举内容放到Map中

编程

将枚举内容放到Map中,简单方法如下

import java.util.Map;

import java.util.TreeMap;

import com.example.demo.utils.JacksonUtils;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)

public class EnumToMapTest {

@Test

public void test() {

Map<String, PayType> toMap = PayType.toMap();

System.out.println(JacksonUtils.beanToJson(toMap));

}

enum PayType {

ALIPAY("支付宝扫码", 1),

WEIXIN_PAY("微信扫码", 2);

PayType(String name, int value) {

this.name = name;

this.value = value;

}

public String name;

public int value;

public static Map<String, PayType> toMap() {

Map<String, PayType> map = new TreeMap<>();

PayType[] types = PayType.values();

for (PayType type : types) {

map.put(String.valueOf(type.value), type);

}

return map;

}

}

}

执行测试结果

以上是 将枚举内容放到Map中 的全部内容, 来源链接: utcz.com/z/518621.html

回到顶部