Java's "forEach" to Scala


Question: 

How can the following be converted to scala? If I live it as it is, I'm getting a big type mismatch expected

.entrySet().forEach(entry -> {..})

I tried specifying entry to java.util.Map.Entry, or changing to scala foreach, doesn't work.

Let me know if you need any more info/code, and I'll create some dummy example since I'm not allowed to post the exact code.




2 Answers: 

check this

Iterating over Java collections in Scala

for each in scala

for(entry<-entrySet){
//
}

or

entrySet.foreach{entry=>
//
}

or

.entrySet.map{entry => 
//
}
 

Just

import scala.collection.JavaConversions._

for an implicit conversion to scala collections.

 

More Articles


Apply function to one element of a list in Python

I'm looking for a concise and functional style way to apply a function to one element of a tuple and return the new tuple, in Python.For example, for the following input:inp = ("hello", "my", "friend")I would like to be able to get the following output:out = ("hello", "MY", "friend")I came up with t

scalatest - Confusing type mismatch in Scala

I have:val words = List("all", "the", "words", "all", "the", "counts", "all", "day")val map = Exercise02.count(words.iterator)val expected = Map("all" -> 3, "the" -> 2, "words" -> 1, "counts" -> 1, "day" -> 1)where Exercise02.count is java.util.Iterator[String] => Map[String, Int]

R: turning list items into objects

I have a list of objects that I've created manually, like this:rand1 <- rnorm(1e3)rand2 <- rnorm(1e6)myObjects <- NULLmyObjects[[1]] <-rand1myObjects[[2]] <-rand2names(myObjects) <- c("rand1","rand2")I'm working on some code that bundles up objects and puts them up in S3. Then I ha


macos - uninstall python on mac Mavericks

I'm a new Mac user. I installed python 2.7.6 but I cannot run my scripts (through IDLE or simply a script). I want to reinstall it but I don't want to mess with the version which comes pre-installed on mac.Thank you for your help.note: there are questions on stackOverflow that are similar to mine, y

php - how to set a page active automatically when it is loaded

i want to change the active class when each page loaded. my jquery code is$(document).ready(function () {$('.nav li a').click(function(e) { $('.nav li a.active').removeClass('active'); $(this).addClass('active'); e.preventDefault();});});and my html code is<ul class="nav nav-stacked bg-n

macos - What is the difference between installing python from the website and using brew?

I have a Mac with OSX 10.11.6. I used brew to install python3. It installed python 3.5.2, but I need python 3.5.1. I've been googling, but can't figure out how I would install 3.5.1 via brew. So I went to python.org and downloaded the python-3.5.1-macosx10.6.pkg. I searched for how installing python


java - Hadoop options are not having any effect (mapreduce.input.lineinputformat.linespermap, mapred.max.map.failures.percent)

I am trying to implement a MapReduce job, where each of the mappers would take 150 lines of the text file, and all the mappers would run simmultaniously; also, it should not fail, no matter how many map tasks fail.Here's the configuration part: JobConf conf = new JobConf(Main.class); c

php - Dynamically replace form with message upon submission

I've written a basic PHP script to generate an email when my contact form is submitted, but as it stands, it redirects the user to an error or thankyou page, which is a little clunky. I'd love to dynamically replace the entire form (or, at least, the submit button) with an error or success message.

Multi-user mass image uploads with PHP

I'll soon be building a competition microsite which is based entirely around image uploads: in order to enter, users will need to upload an image.I've previously built a similar website on top of a PHP MVC framework, which worked awesomely up until a couple of hours before the competition was due to

pycharm - pydev debugger: CRITICAL WARNING: This version of python seems to be incorrectly compiled

I can't figure out how to fix this PyCharm warning. I started getting it after I migrated my account to a new Mac. (I also just updated PyCharm.) Another symptom is the PyCharm editor is complaining about references to methods on commands like "logging.info()". Even though when I run my app, it s