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

  • Home
  • SEARCH
  • 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 6341853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:07:25+00:00 2026-05-24T20:07:25+00:00

I am trying to filter a map from strings to ints in scala and

  • 0

I am trying to filter a map from strings to ints in scala and I am running into a weird issue.

If I put the following into REPL:

scala> val myMap = Map("a" -> 1, "b" -> 2, "c" -> 3)
myMap: scala.collection.immutable.Map[java.lang.String,Int] = 
| Map(a -> 1, b -> 2, c -> 3)

It’s ok so far, and this works…

scala> myMap.filter(_._2 > 1)
res9: scala.collection.immutable.Map[java.lang.String,Int] = Map(b -> 2, c -> 3)

but this fails…

scala> myMap.filter((k:java.lang.String, v:Int) => v > 1)
<console>:9: error: type mismatch;
 found   : (java.lang.String, Int) => Boolean
 required: ((java.lang.String, Int)) => Boolean
   myMap.filter((k:java.lang.String, v:Int) => v > 1)

My question is what is going on with the error message and the extra pair of parentheses? If I try putting in an extra set of parentheses I get error: not a legal formal parameter.

  • 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-24T20:07:26+00:00Added an answer on May 24, 2026 at 8:07 pm

    myMap.filter expects a function of type Tuple2[String, Int] => Boolean, which is equivalent to ((String, Int)) => Boolean. You’re passing it a function of type (String, Int) => Boolean; that is, a function that takes two arguments rather than a single Tuple2.

    Two ways to get it to work:

     myMap.filter { case (k, v) => v > 1 }
    

    and

     myMap.filter(Function.tupled((k, v) => v > 1))
    

    The first works by pattern matching, and the second by converting the function (String, Int) => Boolean to ((String, Int)) => Boolean.

    By the way, there has been discussion about unifying tuples and function argument lists. Maybe in a future version of Scala, all functions will take a single parameter (which might be a tuple).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to filter a table from an alphabetical <select> input with jQuery. I
I'm trying to filter my google maps markers from a checkbox, based on this
I am trying to apply a filter to a Map. The intention is to
i'm trying to start a map activity from a normal Activity. But keep getting
I’m trying to convert Spring Security configuration from HTTP namespace into direct configuration using
Im trying to filter rows based on a column say c1 that contains boolean
I am trying to filter an IEnumerable object of the duplicate values, so I
I am trying to filter items with a stored procedure using like . The
I'm trying to filter some XML in JavaScript using E4X and have some specific
I'm trying to filter by a list of values using the criteria API. I

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.