Ok I have this code:
TreeMap<DateTime, Integer> tree2 = getDatesTreeMap();
DateTime startx = new DateTime(startDate.getTime());
DateTime endx = new DateTime(endDate.getTime());
boolean possible = false;
int testValue = 0;
//produces submap
Map<DateTime, Integer> nav = tree2.subMap(startx, endx);
for (Integer capacity : tree2.subMap(startx, endx).values()) {
//Provides an insight into capacity accomodation possibility
//testValue++;
terminals = 20;
if(capacity >= terminals)
possible = true;
else if(capacity < terminals)
possible = false;
}
if(possible == true)
{
for (Integer capacity : tree2.subMap(startx, endx).values()) {
{
capacity -= terminals;
//not sure what to do
}
}
}else{
}
return possible;
It checks for range of date in submap. then checks if values of those dates (which are keys btw) can accomodate terminals (that is reservation number), then if yes it would subtract that from capacity currently in map. I am unsure how to update the capacity in the map for all dates between startx and endx with value
capacity -= terminals;
Thanks, :)
The replace(K key, V value) method of Map interface, implemented by HashMap class is used to replace the value of the specified key only if the key is previously mapped with some value. Parameters: This method accepts two parameters: key: which is the key of the element whose value has to be replaced.
You can use computeIfPresent method and supply it a mapping function, which will be called to compute a new value based on existing one. For example, Map<String, Integer> words = new HashMap<>(); words.
Increase the value of a key in HashMap 2.1 We can update or increase the value of a key with the below get() + 1 method. 2.2 However, the above method will throw a NullPointerException if the key doesn't exist. The fixed is uses the containsKey() to ensure the key exists before update the key's value.
Yes. If a mapping to the specified key already exists, the old value will be replaced (and returned).
You have to reinsert the key / value into the map with the updated value.
tree2.put(key, tree2.get(key) - terminals);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With