首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将这两种方法重构成一种方法?

如何将这两种方法重构成一种方法?
EN

Stack Overflow用户
提问于 2019-08-08 09:26:26
回答 2查看 102关注 0票数 0

摘要作为一种方法,使用lamda表达式如何将枚举合并为Java中的映射

代码语言:javascript
复制
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;
}    

ShopTypeEnumShopBusinessEnum声明

代码语言:javascript
复制
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();

}

//呼叫

代码语言:javascript
复制
    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());
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-08 09:32:29

您可以使用Supplier<Collection<? extends Enum<?>>>来获得要迭代的集合。

代码语言:javascript
复制
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());
}

叫它,

代码语言:javascript
复制
object.get(() -> getAllStoreTypeEnus());
object.get(() -> getShopBusinessEnus());

我注意到您使用的是e.getValue(),它不是Enum接口的一部分。

我假设有一个共同的接口

代码语言:javascript
复制
interface ValueAware {
     String getValue(); 
}

enum ShopTypeEnum implements ValueAware { ... }
enum ShopBusinessEnum implements ValueAware { ... }

这样就可以将参数类型缩小到

代码语言:javascript
复制
Supplier<Collection<? extends Enum<? extends ValueAware>>>

否则,您将得到一个编译错误

代码语言:javascript
复制
map.put(VALUE, e.getValue());
票数 3
EN

Stack Overflow用户

发布于 2019-08-08 09:30:32

也许只要改变一下就行了:

代码语言:javascript
复制
Collection<ShopBusinessEnum> shopBusinessEnums = getShopBusinessEnus();

至:

代码语言:javascript
复制
Collection<? extends Enum> theEnums = ...

因为这样你仍然可以处理某种类型的Enum,但是您是独立于实际类型的。因此,您应该能够很好地使用一段代码来迭代这样的集合。

比如:

代码语言:javascript
复制
public Collection<Map<String, String>> getProperty(Collection<? extends Enum> theEnums) {
  return theEnums.stream().map(....
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57409330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档