[Java] 枚举类型:遍历为List
方式1
Demo
import com.google.common.collect.Lists;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public enum StandardOperationEntityType {
CREATE("CODELIST", "数据字典"),
DELETE("CODELIST_ITEM", "数据字典项");
private final String code;
private final String name;
public final static String CODE_PARAM = "code";
public final static String NAME_PARAM = "name";
StandardOperationEntityType(String code, String name){
this.code = code;
this.name = name;
}
public static StandardOperationEntityType findByCode(String code) {
for (StandardOperationEntityType type : values()) {
if (type.getCode().equals(code)) {
return type;
}
}
return null;
}
public static StandardOperationEntityType findByName(String name) {
for (StandardOperationEntityType type : values()) {
if (type.getName().equals(name)) {
return type;
}
}
return null;
}
public String getCode() {
return this.code;
}
public String getName() {
return this.name;
}
public static List<Map<String, String>> toList() {
List<Map<String, String>> list = Lists.newArrayList();//Lists.newArrayList()其实和new ArrayList()几乎一模
for (StandardOperationEntityType item : StandardOperationEntityType.values()) {
Map<String, String> map = new HashMap<String, String>();
map.put(StandardOperationEntityType.CODE_PARAM, item.getCode());
map.put(StandardOperationEntityType.NAME_PARAM, item.getName());
list.add(map);
}
return list;
}
}
方式2
StandardOperationEntityType [] array = StandardOperationEntityType.values();
List<StandardOperationEntityType> list = Arrays.asList(array);
Set<StandardOperationEntityType> set= new HashSet<>( list );
//Set<StandardOperationEntityType> set= new HashSet<>( Arrays.asList(StandardOperationEntityType.values()) );
X 参考文献
本文作者:
千千寰宇
本文链接: https://www.cnblogs.com/johnnyzen
关于博文:评论和私信会在第一时间回复,或直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
日常交流:大数据与软件开发-QQ交流群: 774386015 【入群二维码】参见左下角。您的支持、鼓励是博主技术写作的重要动力!
本文链接: https://www.cnblogs.com/johnnyzen
关于博文:评论和私信会在第一时间回复,或直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
日常交流:大数据与软件开发-QQ交流群: 774386015 【入群二维码】参见左下角。您的支持、鼓励是博主技术写作的重要动力!