簡體   English   中英

比較Java中的兩個列表

[英]Comparing two Lists in java

在一個應用程序中,我們的登機表具有(boarding_id + invoice_no + item_id)作為鍵。 具有與Boarding相同的表結構的Boarding_tmp表。 我們正在從csv文件中加載Boarding_tmp(管道分隔符列值)

我們有一個包含所有列的登機Java Bean。 假設我們有兩個清單-

1. boardingList with all active records in Boarding table
2. boardingTmpList with all records in Boarding_Tmp table

我需要比較這兩個列表以實現以下方案-

我們需要將Boarding表與Boarding_tmp表進行比較,並需要執行以下操作(無SQL連接。我們必須在對象級別執行此操作)-

a. All records which are in Boarding_tmp but not in Boarding table should be inserted in Boarding table (means they are new records)
b. All records which are in Boarding but not in Boarding_tmp table should be mark as deactivated in Boarding table (we have a active column in Boarding table)

在Boarding.java中-

public class Boarding implements Comparable {

    private String bordingId;
    private String itemId;
    private String invoiceNo;
    private String itemName;
    private long qty;
    private double price;

    /**
     * @return the bordingId
     */
    public String getBordingId() {
        return bordingId;
    }

    /**
     * @param bordingId
     *            the bordingId to set
     */
    public void setBordingId(String bordingId) {
        this.bordingId = bordingId;
    }

    /**
     * @return the itemId
     */
    public String getItemId() {
        return itemId;
    }

    /**
     * @param itemId
     *            the itemId to set
     */
    public void setItemId(String itemId) {
        this.itemId = itemId;
    }

    /**
     * @return the invoiceNo
     */
    public String getInvoiceNo() {
        return invoiceNo;
    }

    /**
     * @param invoiceNo
     *            the invoiceNo to set
     */
    public void setInvoiceNo(String invoiceNo) {
        this.invoiceNo = invoiceNo;
    }

    /**
     * @return the itemName
     */
    public String getItemName() {
        return itemName;
    }

    /**
     * @param itemName
     *            the itemName to set
     */
    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    /**
     * @return the qty
     */
    public long getQty() {
        return qty;
    }

    /**
     * @param qty
     *            the qty to set
     */
    public void setQty(long qty) {
        this.qty = qty;
    }

    /**
     * @return the price
     */
    public double getPrice() {
        return price;
    }

    /**
     * @param price
     *            the price to set
     */
    public void setPrice(double price) {
        this.price = price;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((bordingId == null) ? 0 : bordingId.hashCode());
        result = prime * result
                + ((invoiceNo == null) ? 0 : invoiceNo.hashCode());
        result = prime * result + ((itemId == null) ? 0 : itemId.hashCode());
        return result;
    }

    @Override
    public int compareTo(Object o) {
        if (this == o) {
            return 0;
        }
        if (o == null) {
            return 1;
        }
        if (getClass() != o.getClass()) {
            return 1;
        }
        Boarding other = (Boarding) o;
        if (bordingId == null) {
            if (other.bordingId != null) {
                return 1;
            }
        } else if (!bordingId.equals(other.bordingId)) {
            return 1;
        }
        if (invoiceNo == null) {
            if (other.invoiceNo != null) {
                return 1;
            }
        } else if (!invoiceNo.equals(other.invoiceNo)) {
            return 1;
        }
        if (itemId == null) {
            if (other.itemId != null) {
                return 1;
            }
        } else if (!itemId.equals(other.itemId)) {
            return 1;
        }

        return 0;
    }

}

請幫忙。 謝謝。

您可以嘗試Collections Util庫。 對於您描述的場景,它們有非常有用的方法。

您應該能夠執行以下操作:

List<Boarding> insertBoarding = new ArrayList(boardingTmpList);
insertBoarding.removeAll(boardingList);
// insert all elements in insertBoarding to the table
List<Boarding> deactivateBoarding = new ArrayList(boardingList);
deactivateBoarding.removeAll(boardingTmpList);
// deactivate all elements in deactivateBoarding in the table

假設您的相等性測試有效,那么應該會產生正確的結果。

注意:您將必須重寫equals方法才能正常工作。

使用List#retainAll方法很容易實現。

javadoc在這里

暫無
暫無

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

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