I was writing some Java 8 Streams code. I have a list of files, but I want to keep all large files over 100 MiB.
// Filtering large files details = details.stream() .filter(fileDetails -> fileDetails.getSize() > HUNDRED_MEBIBYTES) .collect(Collectors.toList());
filter Javadoc says:
Returns a stream consisting of the elements of this stream that match the given predicate.
Therefore I have to pass a predicate describing what I want to keep.
// Filtering large files comment gave me an uneasy feel, as I was hesitating on whether saying "Filtering X" meant keeping X or discarding X. I tried to think about it but then became confused (I've rewritten that comment
// Keep large files only since).
Here's the heart of the question:
If I am filtering water into a cup using a water filter, the cup will contain filtered water, and we discarded impurities.
If I am filtering air entering into a room using a air filter, the room will contain filtered air, and we discarded dust.
But if I am filtering air entering into my computer case using a dust filter, the computer will contain filtered air, and we discarded dust.
It seems that "filtering X" always mean "I want X and discard Y", but a "Z filter" would either mean "I want Z and discard Y" or "I want X and discard Z". Is it true? Is no one ever filtering impurities, or filtering dust?
Following that logic, I am always filtering large files (I would not say in this case I am filtering small files), but it is ambiguous whether I should have a "large files filter" or a "small files filter".
I have to admit, sometimes I don't remember whether the
filter method is meant to have a predicate describing what I want to keep or what I want to discard, for example the following code does not do what one expects at first glance:
allMiddleNames = users.stream() .map(User::getMiddleName) .filter(String::isEmpty) // FIXME: This is wrong, should be "s -> !s.isEmpty()" .collect(Collectors.toSet());
Is there a clear, deterministic use of the word filter, particularly when used with the following expressions?
I am filtering X (
filteringis a present participle)
I have a X filter (
filteris a noun)
My container contains filtered X (
filteredis an adjective)
I have filtered X (
filteredis at the past tense)