public class OrderListEntity {
/**
* @deprecated 对象集合的处理
* @param aData 集合a
* @param bData 集合b
* @return 返回集合a比集合b多出来的部分, 通过id判断
*/
public static List<OrderListEntity> AHasMoreThanBData(
List<OrderListEntity> aData, List<OrderListEntity> bData) {
List<OrderListEntity> moreEntities = new ArrayList<>();
if (aData == null && bData == null) {
return moreEntities;
}
for (OrderListEntity sAData : aData) {
boolean isSame = false;
for (OrderListEntity sBData : bData) {
if (sAData.isTheSame(sBData)) {
isSame = true;
break;
}
}
if (!isSame) {
moreEntities.add(sAData);
}
}
return moreEntities;
}
public OrderListEntity() {
}
public OrderListEntity(int id, String content) {
this.id = id;
this.content = content;
}
private int id;
private String content;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public boolean isTheSame(OrderListEntity entity) {
if (entity == null) return false;
return this.id == entity.getId();
}
@Override
public String toString() {
return "{" +
"id=" + id +
", content='" + content + '\'' +
'}';
}
}
public static void main(String[] args) {
OrderListEntity entity1 = new OrderListEntity(1, "内容1");
OrderListEntity entity2 = new OrderListEntity(2, "内容2");
OrderListEntity entity3 = new OrderListEntity(3, "内容3");
OrderListEntity entity4 = new OrderListEntity(4, "内容4");
OrderListEntity entity5 = new OrderListEntity(5, "内容5");
OrderListEntity entity6 = new OrderListEntity(6, "内容6");
OrderListEntity entity7 = new OrderListEntity(1, "内容1");
List<OrderListEntity> list1 = new ArrayList<>();
list1.add(entity1);
list1.add(entity2);
list1.add(entity3);
list1.add(entity4);
List<OrderListEntity> list2 = new ArrayList<>();
list2.add(entity4);
list2.add(entity5);
list2.add(entity6);
//内容相同的两个对象也属于两个对象,contains是判断对象是否存在,创建另外一个对象,内容相同也是不存在的
System.out.println("是否存在:" + list1.contains(entity7));
System.out.println("是否相同-(equals):" + entity1.equals(entity7));
System.out.println("是否相同-(==):" + (entity1 == entity7));
//通过比较id是否相同判断是否是一个订单
System.out.println("是否相同-(id比较):" + entity1.isTheSame(entity7));
System.out.println("\n");
System.out.println("list1比list2多出来的部分:");
//listEntities1比listEntities2多出来的部分
List<OrderListEntity> orderListEntities1 = OrderListEntity.AHasMoreThanBData(list1, list2);
for (OrderListEntity orderListEntity : orderListEntities1) {
System.out.print(orderListEntity.toString());
}
System.out.println("\n");
System.out.println("list2比list1多出来的部分:");
//listEntities2比listEntities1多出来的部分
List<OrderListEntity> orderListEntities2 = OrderListEntity.AHasMoreThanBData(list2, list1);
for (OrderListEntity orderListEntity : orderListEntities2) {
System.out.print(orderListEntity.toString());
}
}
运行结果: