应用环境:从数据库中查询出满足一系列条件的记录,然后以对象的形式封装到List中去。此时假设有两个条件A和B,满足A的记录集和为ListA,满足B的记录集合为ListB,现在要将ListA和ListB合并为一个List,注意ListA和ListB中可能有重复的记录(因为可能某条记录即满足条件A又满足条件B),要过滤掉重复的记录。
方法过程:我们假设List中存放的对象都是Order对象,属性orderId用于标识一个唯一的Order对象。
List<order> list = new ArrayList<Order>();
if(ListA!=null){
Iterator it= ListA.iterator();
while(it.hasNext()){
list.add((Order)it.next());
}
}
if(ListB!=null){
Iterator it= ListB.iterator();
while(it.hasNext()){
Order o=(Order)it.next();
if(!list.contains(o))
list.add(o);
}
}
首先我们将ListA中的对象全部装入到list中,然后在装入ListB中对象的
时候对ListB中的每个元素进行一下判断,看list中是否已存在该元素,这里我们使用List接口的contains()方法。它的原理是这样的:如上例中的
list.contains(o),系统会对list中的每个元素e调用o.equals(e),方法,加入list中有n个元素,那么会调用n次o.equals(e),只要有一次o.equals(e)返回了true,那么list.contains(o)返回true,否则返回false。因此为了很好的使用contains()方法,我们需要重新定义下Order类的equals方法,根据我们的业务逻辑,如果两个Order对象的orderId相同,那么我们认为它们代表同一条记录,于是equals方法定义如下:
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Order other = (Order) obj;
if(this.getOrderid()!=other.getOrderid())
return false;
return true;
}
这样只要ListB中有一条记录的orderId和list中的某条记录的orderId
相等,那么我们就认为该记录已存在,不再将它放入list,这样就避免了重复记录的存在。
分享到:
相关推荐
List的contains方法、Array的contains方法的使用
NULL 博文链接:https://corrinejtt.iteye.com/blog/650200
C# 有关List<T>的Contains与Equals方法.doc
1:contains判断去重(有序) 此方法的优点的:理解起来比较简单,并且最终得到的集合也是有序的,这里的有序指的是新集合的排列顺序和原集合的顺序是一致的;但缺点是实现代码有点多,不够简洁优雅。 2:迭代器去重...
本篇文章主要是对C#中List<T> Contains()的用法进行了总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助
js中终于有了自己的List,类似于java中的List对象,本文件是源码,亲测有如下方法:add()、value、size()、get(index)、contains(key)、remove(index)、removeAll()。。。
mysql遇见Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre的问题
泛型List中有一个比较列表是否已包含对象的方法Contains(),今天在网上搜了一个用法,记录下来,备查。 要用此方法比较我们的自定义对象,首先要有一个比较器, 要注意的是,这里的比较器是实现IEqualityComparer...
This list contains the head words of the families in the Academic Word List. The numbers indicate the sublist of the Academic Word List. For example, abandon and its family members are in Sublist 8 of...
This list contains the head words of the families in the Academic Word List. The numbers indicate the sublist of the Academic Word List. For example, abandon and its family members are in Sublist 8 of...
A shopping list contains products which can be added, edited and removed as well. Motivation Nowadays there are many apps that require many or all permissions available in Android in order to be ...
如何判断list中是否包含某个值, 很遗憾,contains是不行的,只能穷举了 ${sessionScope.SelectDeviceIdsList} var=map> ${map.key> <c:set var=iscontain value=true /
It contains a configuration model (ALV configuration model), which you can use to determine the appearance, functions, and runtime behavior of the ALV display, as well as the display of the standard ...
虽然您总要创建接口特定的实现,但访问实际集合的方法应该限制在接口方法的使用上;因此,允许您更改基本的数据结构而不必改变其它代码。 · Collection 接口是一组允许重复的对象。 · Set 接口继承 Collection,...
学习java的一些笔记和个人总结 9、Collection 和 Collections的区别。 Collection是集合类的上级...HashMap把Hashtable的contains方法去掉了,改成containsvalue和containsKey。因为contains方法容易让人引起误解。
主要介绍了解决大于5.7版本mysql的分组报错Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated问题,需要的朋友可以参考下
import LinkedList from "linked-list" ; const list = new LinkedList ( ) ; list . add ( 1 ) ; list . add ( 2 ) ; list . contains ( 2 ) ; // true list . remove ( 2 ) ; list . contains ( 2 ) ; // false ...
Chapter 3 - Contains the source code for the Mailing List project. Chapter 4 - Contains the source code for the Search Engine project. Chapter 5 - Contains the source code for the Personal Calender ...
It s a JPEG decoder and need a lot of promotion and correction,This distribution contains the sixth public release of the Independent JPEG Group s free JPEG software. You are welcome to redistribute ...
Contains 方法 确定某元素是否在HashSet中 Exists 方法 确定HashSet是否包含于指定条件相匹配的元素 ExceptWith 方法 从当前HashSet移除指定集合中的所有元素 IntersectWith 方法 修改当前的HashSet对象,以...