Logo

Programming-Idioms

History of Idiom 135 > diff from v27 to v28

Edit summary for version 28 by matthewmcgibbon:
[Java] matching to x rather than another string

Version 27

2019-09-27, 15:41:17

Version 28

2019-09-27, 15:43:10

Idiom #135 Remove item from list, by its value

Remove at most 1 item from list items, having value x.
This will alter the original list or return a new list, depending on which is more idiomatic. If there are several occurrences of x in items, remove only one of them.

Idiom #135 Remove item from list, by its value

Remove at most 1 item from list items, having value x.
This will alter the original list or return a new list, depending on which is more idiomatic. If there are several occurrences of x in items, remove only one of them.

Imports
Java.utils.List
Imports
Java.utils.List
Code
items.stream().findFirst().filter(e -> e == "User").ifPresent(e -> items.remove(e));
Code
items.stream().findFirst().filter(item -> "x".equals(item)).ifPresent(removeIndex -> items.remove(removeIndex));