-2
private List<RolePermission> permissionList = new ArrayList<RolePermission>();
ListIterator<RolePermission> iterator = permissionList.listIterator();
permissionList.remove(iterator.next().getRolePermissionName().contains("http"));

I want to remove those item from list which is containing term "http" . but this code is not working.

Andrew
  • 13,757
  • 13
  • 66
  • 84
  • "this code is not working" is quite vague: Does it compile? Does it throw an exception? What exception? On what line? etc. – assylias Sep 13 '13 at 17:07
  • You should use Iterator.remove() to do this. Check out http://stackoverflow.com/questions/223918/efficient-equivalent-for-removing-elements-while-iterating-the-collection – Alex Pritchard Sep 13 '13 at 17:09

2 Answers2

5

You need to use iterator.remove();

while (iterator.hasNext())
{
   if (iterator.next().getRolePermissionName().contains("http"))
       iterator.remove();
}
Matthew Mcveigh
  • 5,695
  • 22
  • 22
1

The method List#remove(java.lang.Object) is in your case not suitable for the type boolean (autoboxed to Boolean), the type which contains() returns. A ClassCastException is thrown.

you could try this to remove using iterator:

private List<RolePermission> permissionList = new ArrayList<RolePermission>();   
ListIterator<RolePermission> iterator = permissionList.listIterator();
RolePermission rp = iterator.next();
if(rp.getRolePermissionName().contains("http")) {
    iterator.remove();
}
A4L
  • 17,353
  • 6
  • 49
  • 70