Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all the strings in an array list containing certain characters

so i have an array list that contains strings such as:

ArrayList<String> list = new ArrayList<>();
list.add("bookshelf");
list.add("bookstore");
list.add("library");
list.add("pencil");

Now i wanna search and remove all the strings in the arraylist that contain the word "book" in them. As far as i understand list.remove("book"); will only search for the particular string "book" and not the strings that contain the word "book". How can i solve this?

like image 682
Poison_Frog Avatar asked May 18 '26 18:05

Poison_Frog


1 Answers

You can use removeIf like this:

list.removeIf(s -> s.contains("book"));
like image 152
YCF_L Avatar answered May 21 '26 06:05

YCF_L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!