Esta página ainda não foi traduzida, por isso é exibida em inglês. Ajude a traduzir

intersectionBy

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

Iterable<A> intersectionBy<A, B>(B Function(A a) f, Iterable<A> iterable1, Iterable<A> iterable2) FxAsyncIterable<A> intersectionByAsync<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: intersectionBy(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 intersectionBy((a) => a, iterable1, iterable2).

No chain method exists; call the data-first function or its async counterpart. The concurrency marker from .concurrent(n) applies to iterable2, exactly as with intersectionAsync.

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 · compress — filter by a parallel boolean mask