将枚举分成2个不同的哈希映射

时间:2017-04-14 04:30:24

标签: java enums

我有一个-eq枚举,用于区分网络和移动设备的类型:

Error 1:
Failed building wheel for mysql-connector

Error 2:
Command "/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 -u -c "import setuptools, tokenize;__file__='/private/var/folders/ch/vzgxn0n119zggbgnb0_vg5400000gn/T/pip-build-po0zonwm/mysql-connector/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /var/folders/ch/vzgxn0n119zggbgnb0_vg5400000gn/T/pip-q6b3vmy3-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/ch/vzgxn0n119zggbgnb0_vg5400000gn/T/pip-build-po0zonwm/mysql-connector/

我想将它们分成2个不同的HashMap:

Type

我能够遍历整个枚举并将其放在1张地图中,但无法将其分开,我该如何将其分成2个不同的地图?我想知道我是否愿意:

WEB_TYPE1("T1", "Web Type 1"),
WEB_TYPE2("T2", "Web Type 2"),
MOBILE_TYPE1("T3", "Mobile Type 1"),
MOBILE_TYPE2("T4", "Mobile Type 2");

3 个答案:

答案 0 :(得分:1)

嗯,你可以这样做:

1.-创建你的枚举 2.-迭代你的枚举,并使用startswith或正则表达式进行验证(在我的情况下,我使用startsWith) 3.-一旦你有了答案添加到你的hashMap 4.-看看这个

import java.util.HashMap;
import java.util.Map;

enum Type {
     WEB_TYPE1("T1", "Web Type 1"),
     WEB_TYPE2("T2", "Web Type 2"),
     MOBILE_TYPE1("T3", "Mobile Type 1"),
     MOBILE_TYPE2("T4", "Mobile Type 2");

    private String type;
    public String getType() {
        return type;
    }


    public String getName() {
        return name;
    }


    private String name;


    Type(String type,String name) {
        this.type = type;
        this.name = name;
    }

}
public class SeparateHash {

    public static void main(String[] args) {
        Map<String, String> webMap = new HashMap<>();
        Map<String, String> mobileMap = new HashMap<>();
          for (Type status : Type.values()) {
                System.out.println(status);
                if(status.toString().startsWith("WEB_")){
                    webMap.put(status.toString(), status.getName());
                }else{
                    mobileMap.put(status.toString(), status.getName());
                }
            }




Map<String, String> map = webMap;
for (Map.Entry<String, String> entry : map.entrySet())
{
    System.out.println(entry.getKey() + "   " + entry.getValue());
}

Map<String, String> map2 = mobileMap;
for (Map.Entry<String, String> entry : map2.entrySet())
{
    System.out.println(entry.getKey() + "    " + entry.getValue());
}


    }

}

它会给你这个:

WEB_TYPE1
WEB_TYPE2
MOBILE_TYPE1
MOBILE_TYPE2
WEB_TYPE2   Web Type 2
WEB_TYPE1   Web Type 1
MOBILE_TYPE2    Mobile Type 2
MOBILE_TYPE1    Mobile Type 1

答案 1 :(得分:1)

您可以这样做:

public enum Type {
    WEB_TYPE1("T1", "Web Type 1"),
    WEB_TYPE2("T2", "Web Type 2"),
    MOBILE_TYPE1("T3", "Mobile Type 1"),
    MOBILE_TYPE2("T4", "Mobile Type 2");

    private final String a;
    private final String b;

    Type(String a, String b) {
        this.a = a;
        this.b = b;
    }

    public String getA() {
        return a;
    }

    public String getB() {
        return b;
    }    
}

public class EnumTest {

    public static void main(String[] args) {
        Map<String, String> webMap = new HashMap<>();
        Map<String, String> mobileMap = new HashMap<>();

        for (Type t : Type.values()) {
            if (t.getB().contains("Web")) {
                webMap.put(t.getA(), t.getB());
            } else if (t.getB().contains("Mobile")) {
                mobileMap.put(t.getA(), t.getB());
            } 

        }

        for (String s : webMap.keySet()) {
            System.out.printf("key: %s, value: %s\n", s, webMap.get(s));
        }

        for (String s : mobileMap.keySet()) {
            System.out.printf("key: %s, value: %s\n", s, mobileMap.get(s));
        }
    }

}

答案 2 :(得分:1)

为什么不将网页和移动设备封装为枚举类型,那么您可以在Type枚举中自由创建这些类型的多种变体?

例如:

import java.util.Map;
import java.util.HashMap;
import java.util.Collections;

public class EnumTest {

    public static void main(String[] args) {
        System.out.println(Type.WEB_MAP);
        System.out.println(Type.MOBILE_MAP);
    }

    enum DeviceType {
        WEB,
        MOBILE;
    }

    enum Type {
        T1(DeviceType.WEB, "Web Type 1"),
        T2(DeviceType.WEB, "Web Type 2"),
        T3(DeviceType.MOBILE, "Mobile Type 1"),
        T4(DeviceType.MOBILE, "Mobile Type 2");

        public final DeviceType devType;
        public final String description;

        public static final Map<Type, String> WEB_MAP;
        public static final Map<Type, String> MOBILE_MAP;

        Type(DeviceType devType, String description) {
            this.devType = devType;
            this.description = description;
        }

        static {
            Map<Type, String> web = new HashMap<>();
            Map<Type, String> mob = new HashMap<>();

            for (Type t : Type.values()) {
                switch (t.devType) {
                    case WEB:
                        web.put(t, t.description);
                        break;
                    case MOBILE:
                        mob.put(t, t.description);
                        break;
                }
            }

            WEB_MAP = Collections.unmodifiableMap(web);
            MOBILE_MAP = Collections.unmodifiableMap(mob);
        }
    }
}

输出:

{T1=Web Type 1, T2=Web Type 2}
{T3=Mobile Type 1, T4=Mobile Type 2}