2

I have the following list from my configuration:

val markets = Configuration.getStringList("markets");

To create a sequence out of it I write this code:

JavaConverters.asScalaIteratorConverter(markets.iterator()).asScala.toSeq

I wish I could do it in a less verbose way, such as:

markets.toSeq

And then from that list I get the sequence. I will have more configuration in the near future; is there a solution that provides this kind of simplicity?

I want a sequence regardless of the configuration library I am using. I don't want to have the stated verbose solution with the JavaConverters.

5
  • which Configuration libarary is that? Commented Mar 15, 2018 at 10:23
  • com.typesafe.config, it is config-1.3.1.jar it helps me to get values from my configuration files Commented Mar 15, 2018 at 10:26
  • the question is already there stackoverflow.com/questions/17913215/… Commented Mar 15, 2018 at 10:27
  • Possible duplicate of How to get a list with the Typesafe config library Commented Mar 15, 2018 at 10:27
  • Nop I want a Sequence out of it... I don't need a list, and I want as simple as defined by @Evgeny Commented Mar 15, 2018 at 10:28

2 Answers 2

6

JavaConversions is deprecated since Scala 2.12.0. Use JavaConverters; you can import scala.collection.JavaConverters._ to make it less verbose:

import scala.collection.JavaConverters._

val javaList = java.util.Arrays.asList("one", "two")
val scalaSeq = javaList.asScala.toSeq
Sign up to request clarification or add additional context in comments.

Comments

1

Yes. Just import implicit conversions:

import java.util

import scala.collection.JavaConversions._

val jlist = new util.ArrayList[String]()
jlist.toSeq

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.