gpt4 book ai didi

java - 初始化 EnumMap 的映射

转载 作者:行者123 更新时间:2023-11-30 07:19:30 26 4
gpt4 key购买 nike

我需要在 JavaFX 应用程序中初始化以下私有(private)成员,我正在尝试组织 gui 小部件,但我不知道正确的语法,有人可以让我知道正确的语法吗:

这是我用于 EnumMap 的枚举

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

这里是我尝试使用此 map 或基于服务名称键的 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)成员

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

我可以使用以下方法初始化顶级空 mServiceWidgetMap:

mWidgetInfo= new HashMap<>();

我知道我需要将 EnumMap 初始化为 new EnumMap<>(Connection.class);但我还需要将成对的小部件放入这些 EnumMap 的值大小中,但我对如何执行此操作感到困惑。

但是我不知道如何初始化 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);
}

最佳答案

我终于弄清楚如何初始化上面的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);
});
});

关于java - 初始化 EnumMap 的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37819099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com