【问题标题】:How to get a key value and do some action with the value in a hashmap如何获取键值并对哈希图中的值执行一些操作
【发布时间】:2020-04-09 19:06:11
【问题描述】:
DatePicker dp = new DatePicker();
        dp.setDayCellFactory(picker -> new DateCell() {
            public void updateItem(LocalDate date, boolean empty) {
                super.updateItem(date, empty);
                LocalDate today = LocalDate.now();

                setDisable(empty || date.compareTo(today) < 0 );

            }
        });
        /*This switch button will only allow to switch scenes if a date is selected*/
        Button swtich = new Button("Add other details");
        swtich.setOnAction(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent e) {
                if (dp.getValue() == null)
                {
                    a.setAlertType(Alert.AlertType.WARNING);
                    a.setHeaderText("Enter a proper date !");
                    a.showAndWait();


                }
                else {
                    LocalDate localDate = dp.getValue();
                    stage01.setScene(scene01);
                }


            }
        });

现在,只要用户输入其他所需的详细信息,localdate 就会与座位号一起保存在哈希图中

DateDetails.put(dp.getValue(),seatC);

现在我想将存储在 DateDetails 中的日期与用户获取的日期进行比较。如果它已经存储在哈希图中,那么我想获取那个座位的座位 C,然后禁用以前存储的座位

This is the output i get when i save the required details in the hashmap,so everything is fine with the hashmap but i dont have an idea on how to compare the new chose date with the previous stored date

【问题讨论】:

  • 首先,如果您能阐明实际行为是什么并从您的项目中发布其他相关代码而不仅仅是选择,那就太好了。但是据我所知,您的问题是您正在检查两个格式不同的字符串。在你的 if 语句打印出你正在比较的所有内容之前,看看它是否正确。
  • @TagHoward 好的,谢谢你,我试着检查一下 if 语句,我对 java 很陌生,所以请多多包涵,非常感谢你在这方面帮助我。
  • @kleopatra 是的,当然
  • @TagHoward 继承了新代码

标签: java arrays javafx hashmap


【解决方案1】:

您是否尝试过使用.keySet().keySet() 为您提供了一个键数组,然后您可以遍历该键数组来执行您想要的操作。

例如,假设您有一个名为 food 的 HashMap

food.put("apple", 1);
food.put("orange", 2):

然后您可以通过以下方式遍历它们的键:

for(String k: food.keySet()) {
//do whatever
}

【讨论】:

    猜你喜欢
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 2013-01-09
    • 2014-02-20
    • 1970-01-01
    • 2021-12-30
    相关资源
    最近更新 更多