移除list集合中指定对象

1.使用了list.remove(obj),没有效果;

2.使用了removeAll也不起作用:

  List<User> userRemove = new ArrayList<User>();
        //找出要删除的用户
        for (User result : list)
        {
            if (result.getId() == 1 || result.getId() == 3)
            {
                userRemove.add(result);
            }
        }
        list.removeAll(userRemove);

3.最后使用Iteator实现将指定对象从集合中移除出去:

   Iterator<User> it =list.iterator();
        while (it.hasNext())
        {
        	User userObj = it.next();
            if (StringUtils.isBlank(userObj.getProjects()))
            {
                it.remove();  
            }
        }

猜你喜欢

转载自blog.csdn.net/shenxiaomo1688/article/details/88060583
今日推荐