Remove a word from the list by ignoring case sensitive

Javazzs :

I want to delete all occurrences of a word in the ArrayList from a given string .

I have 3 buttons on my frame. One that adds words, a second that removes words, and the third that shows words.

I have a textbox with name textvalue and array list with name mylist

I used:

 textValue = text.getText().toLowerCase().trim();
 if (mylist.contains(textValue)) { 
                  mylist.removeAll(Arrays.asList(textValue)); 
                 label.setText("All occurrences of " + textValue + "removed");
                        } else {
                            label.setText("Word not found.");
                        }

If I put for example : mark and MARK , it will still only delete mark.

I have also tried:

textValue = text.getText().toLowerCase().trim();
                            for (String current : mylist) {
                                if (current.equalsIgnoreCase(textValue)) {
                                    mylist.removeAll(Collections.singleton(textValue));
                                    label.setText("All occurrences of " + textValue + " removed");
                                } else {
                                    label.setText("Word not found.");
                                }

                            }
Deadpool :

Just use removeIf

mylist.removeIf(value->value.equalsIgnoreCase(textValue));

removeIf accepts Predicate as an argument, So you are defining corresponding lambda expression to delete all values that matches to textValue by ignoring case sensitive

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=322539&siteId=1