Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert scala Array to Java array []

Tags:

scala

I have java package There is Host class and Client class that can reveive many hosts

public final class Host {
   public final String name;
   public final int port; 
} 

public class SomeClient{...
   public Client(Host... hosts) throws Exception {
  ....
}

I'm writing scala code to creating this client

//  the hosts example is hosts1,host2,host3
def getClient(hosts:String) :SomeClient ={
   val default_port:Int = 100
   val hostsArr: Array[String] =hosts.split(",")
   new Client (hostArr ???)
 }

How can I map and convert scala array of strings to Host[], so the client will be created properly?

like image 290
Julias Avatar asked Dec 24 '22 13:12

Julias


1 Answers

def getClient(hosts:String): SomeClient = {

    val default_port:Int = 100
    val hostsArr: Array[String] = hosts.split(",")

    //Here you map over array of string and create Host object for each item 
    val hosts = hostsArr map { hostStr =>
        val host = new Host()
        //Rest of assignment

        host
    }

    new Client(hosts:_*)
}

You can check the scala reference for repeated parameters section 4.6.2

like image 190
Fatih Donmez Avatar answered Jan 04 '23 22:01

Fatih Donmez