本页尚未翻译,因此以英文显示。 参与翻译

intersection

difference's opposite: elements of the second iterable that ARE also found in the first.

Iterable<A> intersection<A>(Iterable<A> iterable1, Iterable<A> iterable2) FxAsyncIterable<A> intersectionAsync<A>(FxAsyncIterable<A> iterable1, FxAsyncIterable<A> iterable2) Fx<T> Fx<T>.intersection(Iterable<T> other) // chain: values of the chain also in other

Lecture

intersection(iterable1, iterable2) shares difference's argument-order convention: the result walks iterable2, and keeps each element (in iterable2's order, deduplicated) that is found in iterable1. iterable1 is only ever used as a membership set — swap the two arguments and you get the same values in a set-theory sense, but the actual order (and which collection's duplicates get collapsed) is different, since the source of the yielded elements changes. See Demo 1.

Under the hood it's intersectionBy((a) => a, iterable1, iterable2) — reach for intersectionBy directly when you need to match by a computed key across two lists of full records rather than by value equality.

There's no chain method; call the data-first function. On the async side, the concurrency marker from .concurrent(n) applies to iterable2iterable1 is drained up front to build the membership set.

Demo 1 · Basics & argument order

Demo 2 · Async, with concurrency

Try it yourself

Exercise: use intersection to find which of the candidate's skills are actually required.

Related: difference — the exclusion counterpart · intersectionBy — match by a computed key instead · uniq — dedupe a single iterable · includes — test membership of a single value