Skip to content

Instantly share code, notes, and snippets.

@jessestuart
Created March 10, 2020 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessestuart/5294f42ba19294692f20694a4e2a5c9e to your computer and use it in GitHub Desktop.
Save jessestuart/5294f42ba19294692f20694a4e2a5c9e to your computer and use it in GitHub Desktop.
stackoverflow-60622844
import java.time.LocalDate
public class AmountModel {
private Integer id;
private Double amount;
// Not defined in OP
// private CategoryModel categoryModel;
private LocalDate localDate;
}
/* ******************** */
public class MyBudget {
// Map "amounts" by ID for easy O(1) lookup.
Map<Integer, AmountModel> amountMap
// Load some dummy data.
MyBudget() {
List<AmountModel> models = (1..10).collect { i -> new AmountModel(id: i, amount: i) }
this.amountMap = models.collectEntries(model -> [(model.id): model])
}
public void deleteAmount(Integer id) {
if (!amountMap.containsKey(id)) {
// (Handle invalid input)
throw new Exception()
}
amountMap.remove(id)
return
}
}
/* ******************** */
MyBudget budget = new MyBudget()
assert budget.amountMap.containsKey(1)
budget.deleteAmount(1)
assert !budget.amountMap.containsKey(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment