如何获取哈希映射中的键值,并对该值执行一些操作

huangapple 未分类评论43阅读模式
英文:

How to get a key value and do some action with the value in a hashmap

问题

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 switchButton = new Button("Add other details");
switchButton.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);
        }
    }
});

/** 
 * Now the localdate gets saved in the hashmap with seat number as soon as the user enters the other required details 
 */
DateDetails.put(dp.getValue(), seatC);

/** 
 * Now I want to compare this date which is stored in DateDetails with a date picked up by the user.
 * If it's already stored in the hashmap, then I want to get the seatC for that seat and disable the previously stored seats.
 */
// [This is the output I get when I save the required details in the hashmap, so everything is fine with the hashmap
// but I don't have an idea on how to compare the newly chosen date with the previously stored date][1]

// The link to the image: [1]: https://i.stack.imgur.com/Slmqi.png

Please note that in the original text, "swtich" should be "switch", and some code elements like a.setAlertType(Alert.AlertType.WARNING); and a.setHeaderText("Enter a proper date !"); are referenced without prior definition. Make sure to define these elements appropriately in your code.

英文:
DatePicker dp = new DatePicker();
        dp.setDayCellFactory(picker -&gt; new DateCell() {
            public void updateItem(LocalDate date, boolean empty) {
                super.updateItem(date, empty);
                LocalDate today = LocalDate.now();

                setDisable(empty || date.compareTo(today) &lt; 0 );

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


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


            }
        });

Now the localdate gets saved in the hashmap with seat number as soon as the user enters the other required details

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

Now i want to compare this date which is stored in DateDetails with a date picked up by the user . if its already stored in the hashmap then i want to get the seatC which is that seats and then disable the previous stored seats

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

答案1

得分: 0

你尝试过使用.keySet()吗?.keySet()会给你一个键(key)的数组,然后你可以通过遍历这个键的数组来执行你想要的操作。

举个例子,假设你有一个名为food的HashMap:

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

那么你可以通过以下方式遍历这些键:

for(String k: food.keySet()) {
//做你需要的操作
}
英文:

Have you tried using .keySet()? .keySet() gives you an array of the keys, and then you could iterate through that array of keys to do your desired operation.

For example, say you have a HashMap called food

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

Then you can just iterate through they keys by:

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

huangapple
  • 本文由 发表于 2020年4月10日 03:06:11
  • 转载请务必保留本文链接:https://java.coder-hub.com/61128470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定