このページはまだ翻訳されていないため、英語で表示されます。 翻訳に参加する

intersectionBy

intersection, comparing by a computed key instead of value equality.

Iterable<A> fxIntersectionBy<A, B>(B Function(A a) f, Iterable<A> iterable1, Iterable<A> iterable2) FxAsyncIterable<A> fxIntersectionByAsync<A, B>(FutureOr<B> Function(A a) f, FxAsyncIterable<A> iterable1, FxAsyncIterable<A> iterable2) Fx<T> Fx<T>.intersectionBy<B>(B Function(T a) f, Iterable<T> other) // chain: values of the chain keyed in other

Lecture

Same shape as differenceBy, opposite condition: fxIntersectionBy(f, iterable1, iterable2) walks iterable2 and keeps each element whose f-key is found among iterable1's f-keys, deduplicated by that key. f applies to both iterables, so both must share the same element type A — only the comparison key type B can differ from A.

This is the natural match for two lists of full records that share an identifying field: a list of "featured" SKUs and a product catalog, a list of active-user IDs and a list of full user objects, and so on. intersection itself is fxIntersectionBy((a) => a, iterable1, iterable2).

The chain method is .intersectionBy(f, other); the receiver is iterable2 (the one that gets walked) and other is iterable1 (the key set). The concurrency marker from .concurrent(n) applies to iterable2, exactly as with fxIntersectionAsync.

Demo 1 · Basics

Demo 2 · Async, with concurrency

Try it yourself

Exercise: use intersectionBy (keyed on 'sku') to find products that are on sale.

Related: intersection — the value-equality version · differenceBy — exclude by a shared computed key instead · uniqBy — dedupe a single iterable by key · fxCompress — filter by a parallel boolean mask