It may be helpful to think of Java 8’s filter method as keepIf. In other words, it doesn’t filter out items where the condition is true, it filters them in. Concisely, it will return a list of all items where the condition is true. Python’s filter method and Ruby’s select method behave in the same way. For example:

List<String> names = Arrays.asList("larry", "sergey", "eric", "conor");
List<String> selectedNames = names.stream().filter(name -> !name.equals("conor"));
selectedNames.forEach(System.out::println)
// prints larry, sergey, eric

My motivation for writing this post is that I frequently second guess myself when reading code that uses filter. I forget if it means filter out or filter in.

I started to wonder why I have a mental block around this concept, so I dug a little deeper. To me, in modern English the phrase “filter out” is much more common than “filter in”:

  1. Glasses that filter out UV rays
  2. Filter out impurities
  3. Plants and trees filter carbon dioxide out of the air and produce oxygen

Notice in the third sentence “out” comes a couple words after “filter”. I don’t know about you, but when I read “filter carbon dioxide” in the third sentence I automatically assumed something was being filtered out, not in.

So I decided to research this with Google’s Ngram tool. The results were unexpected: it turns out that “filter out” is indeed more common than “filter in”, but the usage of the two phrases is much closer than I expected.

Filter In vs Filter Out Ngram
Data from 1900-2008

The result was unexpected but helpful because it showed me that I need to expand my mental model of how the word “filter” is used.