两个List过滤的问题 对象List集合求值比较 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先循环一遍list1,每取出一个对象时,就循环一遍list2,也就是两层遍历,然后用点标记法比较两个对象的属性值,如果其name属性一致,就在list2中删除。 可以考虑重写你那个对象的equals方法:name字段相等则返回true这样可以使用list2.removeAll(list1)当然,重写equals方法要慎重。 大神 能不能写下代码 我好理解点自己动手丰衣足食,而且是很简单的几行代码就能搞定的。把你的POJO对象的equals方法重写一下,用name做判断就可以了。2#的想法源自,List的remove源码(很简单就看懂了): public boolean remove(Object o) { if (o == null) { for (int index = 0; index < size; index++) if (elementData[index] == null) { fastRemove(index); return true; } } else { for (int index = 0; index < size; index++) if (o.equals(elementData[index])) { fastRemove(index); return true; } } return false; } 直接list2中删除,会报数组越界的错误额。 应该建一个新的集合,把相等的对象放到新的集合中,然后再用list2删除新的集合。 for(Object o:list1){ set.add(o.getName());}list3 = list2.clone();for(Object o:list2){ if(set.contains(o.getName()) { list3.remove(o); }} // 商户列表中剔除vip商户 List<MzStore> deleteList = new ArrayList<MzStore>(); for (MzStore store : storeList) { for (MzStore reStore : recommendStoreList) { if (store.getStoreId() == reStore.getStoreId()) { deleteList.add(store); } } } storeList.removeAll(deleteList); 这个很好做啊(以下代码的参数检查请自行补全): private static class Demo { private String name; private int age; public Demo (String name, int age) { this.name = name; this.age = age; } public String getName() { return this.name; } @Override public String toString() { return "Demo [name=" + name + ", age=" + age + "]"; } } public static void main(String[] args) throws Exception { List<Demo> list1 = new ArrayList<Demo>(); list1.add(new Demo("a", 1)); list1.add(new Demo("b", 2)); List<Demo> list2 = new ArrayList<Demo>(); list2.add(new Demo("a", 1)); list2.add(new Demo("c", 3)); List<Demo> itemsToRemove = new ArrayList<Demo>(); for (Demo demo : list2) { if (isIn(list1, demo)) { itemsToRemove.add(demo); } } list2.removeAll(itemsToRemove); System.out.println(list2); //[Demo [name=c, age=3]] } private static boolean isIn(List<Demo> list, Demo target) { for (Demo d : list) { if (d.getName().equals(target.getName())) { return true; } } return false; } 这个难道又是IE的BUG?? LI标签 2px的间距 集合在页面上取值的问题? 快毕业了,想做java开发,各位给点意见啊? hibernate处理时间,高手请进 替换问题 JSF 自定义一个component的问题 高手进来帮我看看这个问题 ejb在jboss上的问题......(在jboss+ejb有经验的朋友请教了!) ★★struts-config.xml文件里的问题★★ 请教jb7连接ora9i问题?在线等 servlet下载前台等待以后,下载文件丢失 java poi导出Excel时设置单元格类型为数值类型问题
这样可以使用list2.removeAll(list1)当然,重写equals方法要慎重。
大神 能不能写下代码 我好理解点自己动手丰衣足食,而且是很简单的几行代码就能搞定的。
把你的POJO对象的equals方法重写一下,用name做判断就可以了。
2#的想法源自,List的remove源码(很简单就看懂了):
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
应该建一个新的集合,把相等的对象放到新的集合中,然后再用list2删除新的集合。
{
set.add(o.getName());
}
list3 = list2.clone();
for(Object o:list2)
{
if(set.contains(o.getName())
{
list3.remove(o);
}
}
List<MzStore> deleteList = new ArrayList<MzStore>();
for (MzStore store : storeList) {
for (MzStore reStore : recommendStoreList) {
if (store.getStoreId() == reStore.getStoreId()) {
deleteList.add(store);
}
}
} storeList.removeAll(deleteList);
private String name;
private int age;
public Demo (String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
@Override
public String toString() {
return "Demo [name=" + name + ", age=" + age + "]";
}
}
public static void main(String[] args) throws Exception {
List<Demo> list1 = new ArrayList<Demo>();
list1.add(new Demo("a", 1));
list1.add(new Demo("b", 2));
List<Demo> list2 = new ArrayList<Demo>();
list2.add(new Demo("a", 1));
list2.add(new Demo("c", 3));
List<Demo> itemsToRemove = new ArrayList<Demo>();
for (Demo demo : list2) {
if (isIn(list1, demo)) {
itemsToRemove.add(demo);
}
}
list2.removeAll(itemsToRemove);
System.out.println(list2); //[Demo [name=c, age=3]]
}
private static boolean isIn(List<Demo> list, Demo target) {
for (Demo d : list) {
if (d.getName().equals(target.getName())) {
return true;
}
}
return false;
}