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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:44:11+00:00 2026-06-04T01:44:11+00:00

I use scala.collection.immutable.HashMap<A,B> from some Java code and would like to use the Scala-native

  • 0

I use scala.collection.immutable.HashMap<A,B> from some Java code and would like to use the Scala-native method toArray() to convert the contents (or values) of the map to an array.

I currently use JavaConversions.asMap() etc, and then use the traditional, but ugly, Java toArray(T[]) methods but I would prefer to call the Scala built in method directly instead.

This must be done from Java. Rewriting the code in Scala is not an option.

I am using Scala 2.9.1.

  • 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-04T01:44:13+00:00Added an answer on June 4, 2026 at 1:44 am

    You need to supply a ClassManifest for the array type, T. This is available from the companion object (see note) for ClassManifest. So:

    itr.toArray(ClassManifest$.MODULE$.fromClass(T.class));
    

    In this example, T is a real type (not a type parameter). So for example, if itr were a Seq[String], you would use this;

    itr.toArray(ClassManifest$.MODULE$.fromClass(String.class));
    

    Because scala’s Map is actually a bunch of tuples, you would use this:

    map.toArray(ClassManifest$.MODULE$.fromClass(Tuple2.class));
    

    Of course, this gives you a Tuple2[], rather than a Tuple2<K,V>[] for the key and values types K and V respectively. As you are in Java-land, you can cast the raw type


    Note: accessing the companion object from Java.

    The companion object of a type M is available by accessing the static field M$.MODULE$

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

Sidebar

Related Questions

Can I use scala List in Java, like : import scala.collection.immutable.List; class HelloScalaList {
Why do some method descriptions in Scaladoc start with [use case] ? Example: scala.collection.immutable.StringOps.++
I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but
I've grabbed some Scala CSV parsing code from here: Use Scala parser combinator to
I'm writing some Scala code which uses the Apache POI API. I would like
Methods like aggregate on scala.collection.parallel.immutable.ParIterableLike process using Threads. Does that mean I shouldn't be
Say I have some Scala code like this: // Outputs 1, 4, 9, 16,
I'd like to convert a scala map with a Boolean value to a java
I am trying to use the new immutable OptionParser in the Scala scopt 2.0.1
Our company produces a number of Java / Scala libraries for use by our

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.