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

pick

Returns a copy of a Map with only the given keys.

Map<K, V> pick<K, V>(Iterable<K> keysToPick, Map<K, V> map)

Lecture

pick is omit's mirror: instead of listing what to remove, you list what to keep. Requested keys that aren't present in the source map are simply absent from the result — pick does not insert them with a null placeholder, so the result's key set can be smaller than keysToPick.

Reach for it whenever you want a narrow "view" of a larger map: a public API response derived from an internal record, a summary row derived from a full one, and so on.

Demo 1 · Basics

Demo 2 · Slimming a whole list

Try it yourself

Exercise: use pick to keep only 'id' and 'email' from profile.

Related: omit — the inverse: drop only some keys · pickBy — keep by predicate instead of key list · props — pull several values out as a List · prop — pull a single value out