初始化 EnumMap 的 Map

4

我需要在一个JavaFX应用程序中初始化以下私有成员,我试图组织gui小部件。然而,我不知道正确的语法,能否有人告诉我正确的语法:

这是我用于EnumMap的枚举:

enum Connection {
    Connection1,
    Connection2,
    Connection3,
    Connection4;
}

这里是我试图用地图或基于服务名称键的EnumMaps来组织的小部件选择,因此以下4个复选框和标签列表归为一组(我有类似的JavaFX小部件用于服务2等)。
@FXML
private CheckBox mService1CheckBox1;
@FXML
private CheckBox mService1CheckBox2;
@FXML
private CheckBox mService1CheckBox3;
@FXML
private CheckBox mService1CheckBox4;
@FXML
private Label mService1Label1;
@FXML
private Label mService1Label1;
@FXML
private Label mService1Label1;
@FXML
private Label mService1Label1;

这是我正在尝试使用JavaFX小部件初始化的私有成员

private Map<String, EnumMap<Connection, AbstractMap.SimpleEntry<Label,
CheckBox>>> mWidgetInfo;

我可以使用以下代码初始化顶层空的 mServiceWidgetMap:

mWidgetInfo= new HashMap<>();

我知道我需要将EnumMaps初始化为new EnumMap<>(Connection.class); 但是我还需要在这些EnumMaps的值大小中放置小部件对,我不知道该如何做。
然而,我不知道如何初始化enumMap值对。非常感谢语法帮助。
编辑 经过一番努力,我想出了以下方法,但肯定还有更简单的方法,比如双括号初始化或其他不那么冗长的方法。
private void initializeServiceHeartbeatTab() {
    // @JC Todo - dynamically create base on CSV rows
    // Map<String, EnumMap<Connection, SimpleEntry<Label, CheckBox>>>
    EnumMap<Connection, SimpleEntry<Label, CheckBox>> service1Info =
        new EnumMap<>(Connection.class);
    SimpleEntry<Label, CheckBox> pair1 = new SimpleEntry<>(
        mService1Label1, mService1CheckBox1);
    SimpleEntry<Label, CheckBox> pair2 = new SimpleEntry<>(
        mService1Label2, mService1CheckBox2);
    SimpleEntry<Label, CheckBox> pair3 = new SimpleEntry<>(
        mService1Label3, mService1CheckBox3);
    SimpleEntry<Label, CheckBox> pair4 = new SimpleEntry<>(
        mService1Label4, mService1CheckBox4);
    service1Info.put(Connection.Connection1, pair1);
    service1Info.put(Connection.Connection1, pair2);
    service1Info.put(Connection.Connection1, pair3);
    service1Info.put(Connection.Connection1, pair4);

    EnumMap<Connection, SimpleEntry<Label, CheckBox>> service2Info = 
        new EnumMap<>(Connection.class);
    pair1 = new SimpleEntry<>(mService2Label1, mService2CheckBox1);
    pair2 = new SimpleEntry<>(mService2Label2, mService2CheckBox2);
    pair3 = new SimpleEntry<>(mService2Label3, mService2CheckBox3);
    pair4 = new SimpleEntry<>(mService2Label4, mService2CheckBox4);
    service2Info.put(Connection.Connection1, pair1);
    service2Info.put(Connection.Connection1, pair2);
    service2Info.put(Connection.Connection1, pair3);
    service2Info.put(Connection.Connection1, pair4);

    EnumMap<Connection, SimpleEntry<Label, CheckBox>> service2Info = 
        new EnumMap<>(Connection.class);
    pair1 = new SimpleEntry<>(mService3Label1, mService3CheckBox1);
    pair2 = new SimpleEntry<>(mService3Label2, mService3CheckBox2);
    pair3 = new SimpleEntry<>(mService3Label3, mService3CheckBox3);
    pair4 = new SimpleEntry<>(mService3Label4, mService3CheckBox4);
    service3Info.put(Connection.Connection1, pair1);
    service3Info.put(Connection.Connection1, pair2);
    service3Info.put(Connection.Connection1, pair3);
    service3Info.put(Connection.Connection1, pair4);

    mWidgetInfo = new HashMap<>();
    mWidgetInfo.put("albf", service1Info);
    mWidgetInfo.put("fms1", service2Info);
    mWidgetInfo.put("fms2", service3Info);
}
1个回答

3

我终于找到了如何初始化上面的Map的方法。不需要使用所有那些不必要的临时变量,初始化它的诀窍是使用嵌套的双括号初始化,如下:

// Map<String, EnumMap<Connection, SimpleEntry<Label, CheckBox>>>
mWidgetInfo = new HashMap() {{
    put("service1", new EnumMap(Connection.class) {{
        put(Connection.Connection1, new SimpleEntry<>(mService1Label1, mService1CheckBox1));
        put(Connection.Connection2, new SimpleEntry<>(mService1Label2, mService1CheckBox2));
        put(Connection.Connection3, new SimpleEntry<>(mService1Label3, mService1CheckBox3));
        put(Connection.Connection4, new SimpleEntry<>(mService1Label4, mService1CheckBox4));
    }});
    put("service2", new EnumMap(Connection.class) {{
        put(Connection.Connection1, new SimpleEntry<>(mService2Label1, mService2CheckBox1));
        put(Connection.Connection2, new SimpleEntry<>(mService2Label2, mService2CheckBox2));
        put(Connection.Connection3, new SimpleEntry<>(mService2Label3, mService2CheckBox3));
        put(Connection.Connection4, new SimpleEntry<>(mService2Label4, mService2CheckBox4));
    }});
    put("service3", new EnumMap(Connection.class) {{
        put(Connection.Connection1, new SimpleEntry<>(mService3Label1, mService3CheckBox1));
        put(Connection.Connection2, new SimpleEntry<>(mService3Label2, mService3CheckBox2));
        put(Connection.Connection3, new SimpleEntry<>(mService3Label3, mService3CheckBox3));
        put(Connection.Connection4, new SimpleEntry<>(mService3Label4, mService3CheckBox4));
    }});
}};

然后我可以遍历集合中的所有小部件 - 例如,按照以下方式初始化工具提示帮助并设置默认标签:

// initialize the tooltips & default labels
mWidgetInfo.forEach((k, v)-> {
    // no need to switch on the service
    // disable click events on the checkboxes
    // as they should be read only
    v.forEach((k1, widgetPair)-> {
        widgetPair.getKey().setTooltip(
            new Tooltip("Connect ID"));
        CheckBox checkBox = widgetPair.getValue();
        checkBox.setTooltip(new Tooltip("Servicing Connection"));
        checkBox.setOpacity(1);
    });
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接