Remove all Elements With a Custom Null Value Java

1k views Asked by At

So I've been working on some code and I have a custom class named word. From here I have a list of words (dupeWordList) and would like to remove all words from that list that have a value of null. Keep in mind, value is variable inside the word class. The word class contains the following stored values: Frequency (int) Value (String)

Is there anyway to remove all words that when you call word.getValue() it returns null? Surely there is a way to do this. If anything I could loop through the entire list and do this process.

Code:

List<Word> dupeWordList;
dupeWordList = new ArrayList<>(wordList);
dupeWordList.removeAll(Collections.singleton(null));
1

There are 1 answers

1
austin wernli On BEST ANSWER

In java 8 you could do

dupeWordList.removeIf(e -> e.getValue() == null)