gpt4 book ai didi

java - 在java中比较两个列表

转载 作者:行者123 更新时间:2023-12-01 14:55:45 25 4
gpt4 key购买 nike

在应用程序中,我们有一个登机表,其键为( boarding_id +发票_no + item_id )。 Boarding_tmp 表与 Boarding 具有相同的表结构。我们正在从 csv 文件加载 Boarding_tmp(管道分隔符列值)

我们有一个包含所有列的 Boarding 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图书馆。对于您所描述的场景,他们有非常有用的方法。

关于java - 在java中比较两个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14317629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com