簡體   English   中英

初始化EnumMap的地圖

[英]Initialize Map of EnumMap

我需要在嘗試組織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初始化為新的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。 在沒有所有那些不必要的臨時屬性的情況下進行初始化的技巧是使用嵌套的雙括號初始化 ,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM