Mỗi khách hàng có một id và nhiều hóa đơn, có ngày tháng, được lưu dưới dạng Hashmap của khách hàng theo id, của một hashmap hóa đơn theo ngày:
HashMap<LocalDateTime, Invoice> allInvoices = allInvoicesAllClients.get(id);
if(allInvoices!=null){
allInvoices.put(date, invoice); //<---REPEATED CODE
}else{
allInvoices = new HashMap<>();
allInvoices.put(date, invoice); //<---REPEATED CODE
allInvoicesAllClients.put(id, allInvoices);
}
Giải pháp Java dường như được sử dụng getOrDefault
:
HashMap<LocalDateTime, Invoice> allInvoices = allInvoicesAllClients.getOrDefault(
id,
new HashMap<LocalDateTime, Invoice> (){{ put(date, invoice); }}
);
Nhưng nếu get không phải là null, tôi vẫn muốn đặt (ngày, hóa đơn) để thực thi và cũng cần thêm dữ liệu vào "allInvoices ALLCl Client". Vì vậy, nó dường như không giúp được gì nhiều.