解决方案 »

  1.   

    先循环一遍list1,每取出一个对象时,就循环一遍list2,也就是两层遍历,然后用点标记法比较两个对象的属性值,如果其name属性一致,就在list2中删除。
      

  2.   

    可以考虑重写你那个对象的equals方法:name字段相等则返回true
    这样可以使用list2.removeAll(list1)当然,重写equals方法要慎重。
      

  3.   


    大神 能不能写下代码 我好理解点自己动手丰衣足食,而且是很简单的几行代码就能搞定的。
    把你的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;
        }
      

  4.   

    直接list2中删除,会报数组越界的错误额。
         应该建一个新的集合,把相等的对象放到新的集合中,然后再用list2删除新的集合。
      

  5.   

    for(Object o:list1)
    {
      set.add(o.getName());
    }
    list3 = list2.clone();
    for(Object o:list2)
    {
       if(set.contains(o.getName())
      {
         list3.remove(o);
      }
    }
      

  6.   

    // 商户列表中剔除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);
      

  7.   

    这个很好做啊(以下代码的参数检查请自行补全):    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;
        }