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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:32:27+00:00 2026-06-16T06:32:27+00:00

I would like to express all of the following Scala code in Java: object

  • 0

I would like to express all of the following Scala code in Java:

object TupleDemo {
  val tuple = (3, "Hello, world! ")

  /** @return str replicated x times */
  def foo(x: Int, str: String) = str * x

  val tupledFoo1 = (foo _).tupled // partially applied function

  val tupledFoo2 = Function.tupled(foo _) // desugared syntax for same partially applied function
}

object TupleDemoApp extends App {
  import TupleDemo._

  println(tupledFoo1(tuple)) // Hello, world! Hello, world! Hello, world!
  println(tupledFoo2(tuple)) // Hello, world! Hello, world! Hello, world!
}

This is as much as I could figure out for the Java equivalent:

import scala.Function1;
import scala.Function2;
import scala.Tuple2;
import scala.collection.immutable.WrappedString;
import scala.runtime.AbstractFunction2;

public class JavaTupleDemo {
   /** @return str replicated x times */
   static final Function2<Integer, String, String> foo = new AbstractFunction2<Integer, String, String>() {
       public String apply(Integer x, String str) {
           return new WrappedString(str).$times(x);
       }

       // perhaps the types for this method are incorrect?
       public Function1<Tuple2<Integer, String>, String> tupled(Tuple2 tuple2) {
           return null; // what to write here instead of null?
       }
   };

  public static void main(String[] args) {
      // works: Invoke tupled function defined in Scala from Java using Tuple2 defined in Java
      Tuple2<Object, String> tuple = new Tuple2<Object, String>(3, "Hello, World! ");
      System.out.println(TupleDemo.tupledFoo1().apply(tuple));

      // works: Invoke regular function defined in Java from Java
      System.out.println(JavaTupleDemo.foo.apply(3, "Hello, planet! "));

      // stumped: Invoke tupled function defined in Java from Java using both the Scala and the Java Tuple2 instances
  }
}
  • 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-16T06:32:28+00:00Added an answer on June 16, 2026 at 6:32 am

    tupled is implemented in Function2 (and therefore in AbstractFunction2), so there’s no need to define it here—you can just write this:

    import scala.Function;
    import scala.Function2;
    import scala.Tuple2;
    import scala.collection.immutable.WrappedString;
    import scala.runtime.AbstractFunction2;
    
    public class JavaTupleDemo {
      static final Function2<Integer, String, String> foo =
        new AbstractFunction2<Integer, String, String>() {
          public String apply(Integer x, String str) {
            return new WrappedString(str).$times(x);
          }
        };
    
      public static void main(String[] args) {
        Tuple2<Integer, String> tuple =
          new Tuple2<Integer, String>(3, "Hello, World! ");
    
        System.out.println(JavaTupleDemo.foo.tupled().apply(tuple));
        System.out.println(Function.tupled(JavaTupleDemo.foo).apply(tuple));
      }
    }
    

    Note that we need to write foo.tupled(), since Java sees this as a method, and that in both cases we get a Function1, so we have to write .apply(tuple), but otherwise this is essentially the same as the Scala version.


    To address your question about getting the Integer in the tuple typed as an Object: the easiest way to handle this (assuming you can’t or don’t want to make it an Integer explicitly on the Scala side) is probably the following:

    Tuple2<Object, String> scalaTuple = TupleDemo.tuple();
    
    Tuple2<Integer, String> tuple = new Tuple2<Integer, String>(
      (Integer) scalaTuple._1(),
      scalaTuple._2()
    );
    

    I.e., just take the tuple apart, cast the Integer, and put it back together.

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

Sidebar

Related Questions

I have the following legacy code that I would like to mimic, with all
In express.js, I would like to provide an additional attribute on the request object
I have a SQL2005 Express database that I would like to create a copy
Am working in VC++ 2008 (express) and I would like to write something in
I'm using Visual Studio 2008 Express and I would like Visual Studio (or perhaps
What is the best way to express range constraint in JPA? I would like
Would like to make anapplication in Java that will not automatically parse parameters used
First of all, I would like to let you know that I am new
ok, So the query I would like to solve: I would like to express
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode

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.