You do not need them in one line, just use remove to remove the element when it matches:
Iterator<Friend> it = list.iterator(); while (it.hasNext()) { if (it.next().getFriendCaption().equals(targetCaption)) { it.remove();
Full demo:
import java.util.*; public class ListExample { public static final void main(String[] args) { List<Friend> list = new ArrayList<Friend>(5); String targetCaption = "match"; list.add(new Friend("match")); list.add(new Friend("non-match")); list.add(new Friend("match")); list.add(new Friend("non-match")); list.add(new Friend("match")); System.out.println("Before:"); for (Friend f : list) { System.out.println(f.getFriendCaption()); } Iterator<Friend> it = list.iterator(); while (it.hasNext()) { if (it.next().getFriendCaption().equals(targetCaption)) { it.remove();
Conclusion:
$ java ListExample
Before:
match
non-match
match
non-match
match
After:
non-match
non-match
source share