【问题标题】:Initialize Map of EnumMap初始化 EnumMap 的 Map
【发布时间】:2023-03-04 06:57:02
【问题描述】:

我需要在尝试组织 gui 小部件的 JavaFX 应用程序中初始化以下私有成员,但是我不知道正确的语法,请有人告诉我正确的语法:

这是我用于 EnumMap 的枚举

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

这是我尝试使用此地图或 EnumMaps 根据服务名称键组织的小部件的选择,因此以下 4 个复选框和标签的列表属于一起,(我有服务 2 等类似的 JavaFX 小部件。

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

【问题讨论】:

    标签: java javafx enum-map


    【解决方案1】:

    我终于想通了如何初始化上面的 Map。在没有所有那些不必要的临时变量的情况下初始化它的技巧是使用嵌套的double brace initialization,如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多