How can these two methods be refactored into one method Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How can these two methods be refactored into one method without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Abstract as a method, using the lamda expression How to merge Convert an enumeration into a map in 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;
}    

The ShopTypeEnum and ShopBusinessEnum declarations

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();

}


// call

    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());
    }

Answer

You could use a Supplier<Collection<? extends Enum<?>>> to obtain a collection to iterate over.

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());
}

To call it,

object.get(() -> getAllStoreTypeEnus());
object.get(() -> getShopBusinessEnus());

I have noticed that you are using e.getValue() which isn’t a part of the Enum interface.

I am assuming there is a common interface

interface ValueAware {
     String getValue(); 
}

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

so you could narrow the parameter type to

Supplier<Collection<? extends Enum<? extends ValueAware>>>

Otherwise, you would be getting a compilation error on

map.put(VALUE, e.getValue());
We are here to answer your question about How can these two methods be refactored into one method - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji