簡體   English   中英

迭代自定義列表並檢查每個索引處的屬性值

[英]Iterating over custom list and checking the value of attribute at each index

我有一個 POJO:

public class BrokerInvoiceLineItem {
    private Date dealDate;
    private String brokerRefId;
    private String receiverName;
    private double notional;
    private double fixedRate;
    private Date maturityDate;
    private double amount;

}

這些 POJO 的列表通過如下所示的方法獲取:

 List<BrokerInvoiceLineItem> finalBrokerInvoiceLineItemList = brokerInvoice.getLineItems();

如果存儲在列表中的任何 POJO 中的任何字段為空,我需要拋出異常。

我應該如何遍歷 finalBrokerInvoiceLineItemList 並為每個項目檢查上述字段的值,如果其中任何一個為空,則拋出異常。

for-each構造用於迭代 java 中的集合。

for(BrokerInvoiceLineItem item : brokerInvoice.getLineItems()) {
    if(item.getDealDate() == null)
        throw Exception();
}

左邊的參數是將被傳遞到循環目標(可以是塊或語句)的變量,右邊的參數標識要迭代的集合。 在這種情況下,您需要遍歷集合檢查每個項目的日期是否為空,這是通過一個簡單的if語句實現的,如果找到null則拋出異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM