Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7178163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:51:55+00:00 2026-05-28T16:51:55+00:00

Is there a shortcut to filter a Map keeping only the entries where the

  • 0

Is there a shortcut to filter a Map keeping only the entries where the key is contained in a given Set?

Here is some example code

scala> val map = Map("1"->1, "2"->2, "3"->3)
map: scala.collection.immutable.Map[java.lang.String,Int] = Map(1 -> 1, 2 -> 2, 3 -> 3)

scala> map.filterKeys(Set("1","2").contains)
res0: scala.collection.immutable.Map[java.lang.String,Int] = Map(1 -> 1, 2 -> 2)

I am searching for something shorter than this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:51:57+00:00Added an answer on May 28, 2026 at 4:51 pm

    Answering the Question

    You can take advantage of the fact that a Set[A] is a predicate; i.e. A => Boolean

    map filterKeys set
    

    Here it is at work:

    scala> val map = Map("1" -> 1, "2" -> 2, "3" -> 3)
    map: scala.collection.immutable.Map[java.lang.String,Int] = Map(1 -> 1, 2 -> 2, 3 -> 3)
    
    scala> val set = Set("1", "2")
    set: scala.collection.immutable.Set[java.lang.String] = Set(1, 2)
    
    scala> map filterKeys set
    res0: scala.collection.immutable.Map[java.lang.String,Int] = Map(1 -> 1, 2 -> 2)
    

    Or if you prefer:

    scala> map filterKeys Set("1", "2")
    res1: scala.collection.immutable.Map[java.lang.String,Int] = Map(1 -> 1, 2 -> 2)
    

    Predicates

    It’s actually really useful to have some wrapper around a predicate. Like so:

    scala> class PredicateW[A](self: A => Boolean) {
       | def and(other: A => Boolean): A => Boolean = a => self(a) && other(a)
       | def or(other: A => Boolean): A => Boolean = a => self(a) || other(a)
       | def unary_! : A => Boolean = a => !self(a)
       | }
    defined class PredicateW
    

    And an implicit conversion:

    scala> implicit def Predicate_Is_PredicateW[A](p: A => Boolean) = new PredicateW(p)
    Predicate_Is_PredicateW: [A](p: A => Boolean)PredicateW[A]
    

    And then you can use it:

    scala> map filterKeys (Set("1", "2") and Set("2", "3"))
    res2: scala.collection.immutable.Map[java.lang.String,Int] = Map(2 -> 2)
    
    scala> map filterKeys (Set("1", "2") or Set("2", "3"))
    res3: scala.collection.immutable.Map[java.lang.String,Int] = Map(1 -> 1, 2 -> 2, 3 -> 3)
    
    scala> map filterKeys !Set("2", "3")
    res4: scala.collection.immutable.Map[java.lang.String,Int] = Map(1 -> 1)
    

    This can be extended to xor, nand etc etc and if you include symbolic unicode can make for amazingly readable code:

    val mustReport = trades filter (uncoveredShort ∨ exceedsDollarMax)
    
    val european = { 
      val Europe = (_ : Market).exchange.country.region == Region.EU
      trades filter (_.market ∈: Europe)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there shortcut keys to jump between code marked as incorrect (a red wavy
Is there a shortcut in NetBeans to highlight a block of code and comment/uncomment
Is there any shortcut key to switch between .cc and .h in Eclipse CDT?
Is there a shortcut key to bring up the call hierarchy of a method
Is there some shortcut that would allow me after creating method in an interface,
Is there some shortcut way of handling multiple properties on a class (say 50
I am able to set bookmarks in a source file, but are there shortcut
Is there a shortcut for this code? -(IBAction)reset{ button1.hidden=NO; button2.hidden=NO; button3.hidden=NO; button4.hidden=NO; button5.hidden=NO; button6.hidden=NO;
Is there a shortcut for Duplicate Line command in Visual Studio 2008? Some similar
Is there a shortcut to highlight and comment code in textmate, rather than putting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.