java - 这两种方法如何重构为一种方法
问题描述
抽象作为方法,使用 lamda 表达式 How to merge 将枚举转换为 Java 中的映射
public Collection<Map<String, String>> getAllStoreTypeList() {
Collection<ShopTypeEnum> shopTypeEnums = getAllStoreTypeEnus();
List<Map<String, String>> result = shopTypeEnums.stream().map(e -> {
Map<String, String> map = new HashMap();
map.put(VALUE, e.getValue());
map.put(NAME, e.name());
map.put(TAG_PROP_TYPE, TAG_TYPE);
return map;
}).collect(Collectors.toList());
return result;
}
public Collection<Map<String, String>> getAllShopBussinessList() {
Collection<ShopBusinessEnum> shopBusinessEnums = getShopBusinessEnus();
List<Map<String, String>> result = shopBusinessEnums.stream().map(e -> {
Map<String, String> map = new HashMap();
map.put(VALUE, e.getValue());
map.put(NAME, e.name());
map.put(TAG_PROP_TYPE, TAG_TYPE);
return map;
}).collect(Collectors.toList());
return result;
}
ShopTypeEnum
和ShopBusinessEnum
声明_
public enum ShopBusinessEnum implements EnumValueWare {
beauty("美容"),
maintenance("养护"),
fix("维修"),
paint("喷漆"),
metalPlate("钣金"),
certificate("办证"),
violation("违章"),
insurance("保险"),
check("例检"),
boutique("精品"),
repair("抢修"),
vehicleParts("全车件"),
battery("电瓶"),
tire("轮胎"),
decoration("装潢"),
conversion("改装");
private static final Map<String, ShopBusinessEnum> LOOKUP = new LinkedHashMap<>();
static {
for (ShopBusinessEnum shopBusinessEnum : EnumSet.allOf(ShopBusinessEnum.class)) {
LOOKUP.put(shopBusinessEnum.value, shopBusinessEnum);
}
}
private String value;
ShopBusinessEnum(String value) {
this.value = value;
}
public void setValue(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public static ShopBusinessEnum fromValue(String value) {
return LOOKUP.get(value);
}
public static Map<String, ShopBusinessEnum> getLOOKUP() {
return LOOKUP;
}
}
public enum ShopTypeEnum implements EnumValueWare {
_4s("4s店"),
repairShop("维修厂"),
chainStore("连锁店"),
quickRepaired("快修快保店"),
insurancePainting("钣金喷漆"),
carBeauty("汽车美容"),
onlyTire("轮胎专营"),
onlyBattery("电瓶专营"),
modifiedStore("改装店");
private static final Map<String, ShopTypeEnum> LOOKUP = new LinkedHashMap<>();
static {
for (ShopTypeEnum shopTypeEnum : EnumSet.allOf(ShopTypeEnum.class)) {
if (shopTypeEnum.ordinal() == 0) {
shopTypeEnum.setValue(shopTypeEnum.value.replace("_", ""));
}
LOOKUP.put(shopTypeEnum.value, shopTypeEnum);
}
}
private String value;
ShopTypeEnum(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public static ShopTypeEnum fromValue(String value) {
return LOOKUP.get(value);
}
public static Map<String, ShopTypeEnum> getLOOKUP() {
return LOOKUP;
}
}
// common interface
public interface EnumValueWare {
String getValue();
}
// 称呼
public Collection<Map<String, String>> get(Supplier<Collection<? extends Enum<? extends EnumValueWare>>> supplier) {
return supplier.get().stream().map(e -> {
Map<String, String> map = new HashMap();
map.put(VALUE, e.getValue());// error can not resolve method getValue
map.put(NAME, e.name());
map.put(TAG_PROP_TYPE, TAG_TYPE);
return map;
}).collect(Collectors.toList());
}
解决方案
您可以使用 aSupplier<Collection<? extends Enum<?>>>
来获取要迭代的集合。
public Collection<Map<String, String>> get(Supplier<Collection<? extends Enum<?>>> supplier) {
return supplier.get().stream().map(e -> {
Map<String, String> map = new HashMap<>();
// populate
return map;
}).collect(Collectors.toList());
}
称呼它,
object.get(() -> getAllStoreTypeEnus());
object.get(() -> getShopBusinessEnus());
我注意到您使用e.getValue()
的不是Enum
界面的一部分。
我假设有一个通用接口
interface ValueAware {
String getValue();
}
enum ShopTypeEnum implements ValueAware { ... }
enum ShopBusinessEnum implements ValueAware { ... }
所以你可以将参数类型缩小到
Supplier<Collection<? extends Enum<? extends ValueAware>>>
否则,您将收到编译错误
map.put(VALUE, e.getValue());
推荐阅读
- android - 从 Arduino 向 Android/iOS 上的 Flutter 应用发送本地消息?
- python-3.x - 有没有办法单独输入测试和训练数据而不将其拆分为分类器算法?
- python - 如何在虚拟环境中找到安装路径?
- c# - Google api 请求异步方法静默失败,但同步方法在 asp.net 网络服务中工作
- html - ngIf 用户已登录显示组件
- node.js - 是否可以在 Vue、Vuex、Node 中模拟“相当于 firebase”的实时数据库
- c# - 将对象分配给 DataContext 时遇到问题
- java - 如何从外部 jar 自动运行 java 任务?
- sql-server - 这两个查询是否可以视为相同?
- java - 我能知道 Eclipse 如何自动编译 Java 代码吗?