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 3798860
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:38:29+00:00 2026-05-19T13:38:29+00:00

Is it possible to use one call to collect to make 2 new lists?

  • 0

Is it possible to use one call to collect to make 2 new lists? If not, how can I do this using partition?

  • 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-19T13:38:30+00:00Added an answer on May 19, 2026 at 1:38 pm

    collect (defined on TraversableLike and available in all subclasses) works with a collection and a PartialFunction. It also just so happens that a bunch of case clauses defined inside braces are a partial function (See section 8.5 of the Scala Language Specification [warning – PDF])

    As in exception handling:

    try {
      ... do something risky ...
    } catch {
      //The contents of this catch block are a partial function
      case e: IOException => ...
      case e: OtherException => ...
    }
    

    It’s a handy way to define a function that will only accept some values of a given type.

    Consider using it on a list of mixed values:

    val mixedList = List("a", 1, 2, "b", 19, 42.0) //this is a List[Any]
    val results = mixedList collect {
      case s: String => "String:" + s
      case i: Int => "Int:" + i.toString
    }
    

    The argument to to collect method is a PartialFunction[Any,String]. PartialFunction because it’s not defined for all possible inputs of type Any (that being the type of the List) and String because that’s what all the clauses return.

    If you tried to use map instead of collect, the the double value at the end of mixedList would cause a MatchError. Using collect just discards this, as well as any other value for which the PartialFunction is not defined.

    One possible use would be to apply different logic to elements of the list:

    var strings = List.empty[String]
    var ints = List.empty[Int]
    mixedList collect {
      case s: String => strings :+= s
      case i: Int => ints :+= i
    }
    

    Although this is just an example, using mutable variables like this is considered by many to be a war crime – So please don’t do it!

    A much better solution is to use collect twice:

    val strings = mixedList collect { case s: String => s }
    val ints = mixedList collect { case i: Int => i }
    

    Or if you know for certain that the list only contains two types of values, you can use partition, which splits a collections into values depending on whether or not they match some predicate:

    //if the list only contains Strings and Ints:
    val (strings, ints) = mixedList partition { case s: String => true; case _ => false }
    

    The catch here is that both strings and ints are of type List[Any], though you can easily coerce them back to something more typesafe (perhaps by using collect…)

    If you already have a type-safe collection and want to split on some other property of the elements, then things are a bit easier for you:

    val intList = List(2,7,9,1,6,5,8,2,4,6,2,9,8)
    val (big,small) = intList partition (_ > 5)
    //big and small are both now List[Int]s
    

    Hope that sums up how the two methods can help you out here!

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

Sidebar

Related Questions

Is it possible to use @import in one css file like this. @import file1
Is it possible for the XML output of one CGI script to make use
I'm not sure if this is even possible or not, and I can't quite
Possible Duplicate: When should one use final? When should Java programmers prefer to use
Is it possible to use multiple header types in one document? For example: header(Content-type:
Is it possible to use the property of one Spring bean to set the
Possible Duplicate: I have an array of integers, how do I use each one
first time use JTree. Just wondering is it possible to have more than one
Is it possible use a MySQL query to perform this kind of check? If
Is it possible to use the call method of javascript (as described in the

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.