scala - Should I use collectionAsScalaIterable({java collection}) or Seq({java collection}).flatten?


Question: 

We're starting to use Scala Test to test our Java application, and I want to test the contents of a Java Collection. We came up with 2 possibilities:

JavaConversions.collectionAsScalaIterable(getJavaCollection()) must contain(allOf(item1, item2).inOrder)

or

Seq(getJavaCollection()).flatten mustEqual Seq(item1, item2)

Being a beginner to Scala, I'm wondering which way would be better (or is there a better way)?




1 Answer: 

import scala.collection.JavaConverters._
val col = getJavaCollection().asScala //a scala mutable Buffer
col mustEqual Seq(item1, item2)

You can use col as a Seq and perform necessary calculations.

Sometimes it is best to google :)

  1. How can I convert a Java Iterable to a Scala Iterable?
  2. How to convert a java.util.List to a Scala list
  3. Iterating over Java collections in Scala
  4. Converting a Java collection into a Scala collection
 

More Articles


c# - Regular expression to match a string that contains only numbers, not letters

My code is currently using the following Regex expression which matches on numbers:Regex numberExpression = new Regex(@"(?<Number>\d+)");This current works fine for input strings like "1", "100", "1a", "a1", etc....But I want to change it so it does NOT match when the input string contains a l

Dose google play count insalls out of itself downloads?

I recently published my app in google play . The question is how google play show counts ?1 - All device that have google play installed and my apps too , total count of my apps install will show in play store .2 - Just all downloads that directly are downloaded from google play will be count .

apache spark - Scala - How to iterate over tuples on RDD?

I have an RDD that contains tuples like this (A, List(2,5,6,7)) (B, List(2,8,9,10))and I would like to get the index of the first element where a specific condition between value and index holds. So far I have tried this on a single tuple test and it works fine:test._2.zipWithIndex.indexWhere


email - PHP mail function not working based on the body of the message

so on my server I tried running:mail('my@email.com', 'asdf', 'asklfdjksalfdsdaf I know I I know I');and PHP sent the mail perfectly....but then when I changed the message to mail('my@email.com', 'asdf', 'hahahahaa');it did NOT send the email to me....what can possibly cause this? the only thing that

Computer Networks ip addressing

So I have this exercise in one of my classes on network ip addressing. The problem is that i cant figure out how to complete the subnet bits and hosts bits..can anyone help me?the number of hosts is 414, 189, 135 and 90...

Revert from Python 2.7 to Python 2.6 in Mac OSX 10.6.8 doesn't work even though I've uninstalled Python 2.7

I have mac OSX 10.6.8. My system came with Python 2.6I recently installed Python 2.7 but experienced many problems with it so decided to revert back to Python 2.6 by executing $ sudo rm -rf /Library/Frameworks/Python.framework/Versions/2.7I also removed Python 2.7 from my Applications folder. Still


How do I access my website (IIS) from the internet?

I have installed IIS. I have browsed to my website on IIS using the 'Browse *:80 (http)' link in IIS. This navigates to http://localhost. That link is only going to work on my own machine.I don't care about my IP address changing (I'm going to show my website for a few hours max every now and the

plone - How can I get rid of spam users

The company I work for have a small Plone blog. But we have a problem with spam accounts. We use captcha on the site and Plone sends a mail to the users that they must confirm before they can edit the user profile. Still about 600 spam users are created every day. In the Plone user profile they past

python - sklearn.KNeighborsClassifier gives very low accuracy score

I am new to machine learning.I created a data, random numbers in two sets. I am trying how to find a sample, however when doing following, I receive very low accuracy score:from random import randint as Rfrom matplotlib import pyplot as pltimport numpy as npfrom sklearn.neighbors import KNeighborsCl

Scala Collection in JSF

I have developed a Scala and JSF application for learning purpose. In this app I have to convert all my Scala collection objects to Java cllectios before it get rendered in JSF. Is there any easy way this can be achived with something like ScalaElResolver, if yes anybody have a sample code for Scala