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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:00:12+00:00 2026-06-06T21:00:12+00:00

I have the following situation: Java lib class: class LibClass { ArrayList<LibClass> children; }

  • 0

I have the following situation:

Java lib class:

class LibClass {
  ArrayList<LibClass> children;
}

my program Scala code:

abstract class MyClass extends LibClass {
  def boom { }
  def boomMyClassInstances  // ???
}

class Lala extends MyClass
class Hoho extends MyClass

The question is: in the method boomMyClassInstances what is the most Scala-ish way to get all the children of MyClass, stored in the children ArrayList so that I can call the common boom method upon them all?

My try is:

def boomMyClassInstances {
  children.toArray.map { case mc: MyClass => mc.boom }
}

Is this a correct approach? Will it pick all the children of MyClass and this is the right Scala way for that, or do I have to say something about type bounds?

  • 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-06-06T21:00:16+00:00Added an answer on June 6, 2026 at 9:00 pm

    Check out GenTraversableLike.collect. Its signature essentially is Traversable[A].collect[B <: A](f: A ~> B): Traversable[B], that is, it takes a collection with elements of type A and a partial function f from A to B, and returns a collection of static element-type B, where B is a subtype of A.

    val myClassInstances: List[MyClass] =
      children.toList.collect{case mc: MyClass => mc}
    

    Since map expects a total function, your try above will fail with an scala.MatchError in case children does not only contain instances of MyClass. You can work around this by using Option or flatMap, but collect seems to be the most Scala-ish way of achieving what you want.

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

Sidebar

Related Questions

Imagine I have the following situation: Test1.java import java.lang.ref.WeakReference; public class Test1 { public
I have the following situation. I have a Java Class that inherits from another
I have the following situation: I have a java code launching a stored procedure.
I have found following question: $0 (Program Name) in Java? Discover main class? but
I have the following situation. I have a HashMap in Java with keys as
I have following situation - Have a MongoService class, which reads host, port, database
I have following situation. In a constructor of a pseudo class I attach a
I have following situation (simplified, of course): MyDomain.groovy: class MyDomain { MyAnotherDomain anotherDomain //
I have following situation: String a = A Web crawler is a computer program
Following is the code relevant to constructor overloading in Java. Let's have a look

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.