英文:
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 -> 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);
}
}
});
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
答案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("apple", 1);
food.put("orange", 2):
Then you can just iterate through they keys by:
for(String k: food.keySet()) {
//do whatever
}
专注分享java语言的经验与见解,让所有开发者获益!
评论