简体   繁体   English

比较Java中的两个列表

[英]Comparing two Lists in java

In an application we have a Boarding table having ( boarding_id + invoice_no + item_id ) as key. 在一个应用程序中,我们的登机表具有(boarding_id + invoice_no + item_id)作为键。 A Boarding_tmp table with same table structure as Boarding. 具有与Boarding相同的表结构的Boarding_tmp表。 We are loading Boarding_tmp from a csv file (pipe separator column values) 我们正在从csv文件中加载Boarding_tmp(管道分隔符列值)

We have a Boarding java bean with all columns. 我们有一个包含所有列的登机Java Bean。 Let's say we have two list - 假设我们有两个清单-

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

I need to compare these two list to implement below scenario - 我需要比较这两个列表以实现以下方案-

We Need to compare Boarding table with Boarding_tmp table and need to do following (No SQL joins. We have to perform this on object level) - 我们需要将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)

Here in Boarding.java - 在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;
    }

}

Please help. 请帮忙。 Thanks. 谢谢。

You can try Collections Util library. 您可以尝试Collections Util库。 They have very useful methods for the scenario you depicted. 对于您描述的场景,它们有非常有用的方法。

You should be able to do the following: 您应该能够执行以下操作:

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

Assuming your equality tests work, this should yield correct results. 假设您的相等性测试有效,那么应该会产生正确的结果。

NOTE: You will have to override the equals method for this to work properly. 注意:您将必须重写equals方法才能正常工作。

That's easily achivable with use of List#retainAll method. 使用List#retainAll方法很容易实现。

javadoc is here javadoc在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM