Newbie Newbie - 2 months ago 11
Scala Question

How to convert java stream to scala streams?

I am new to scala I am trying to understand the streams. Can I some tell me the difference between jsva 8 streams filter and streams in scala ?

How do I convert this in scala ?

For example if I want to convert this to scala do I need to use to Stream or Stream[] ?

itemList.getIds() is

Map<String, List<Ids>>
:

itemList.getIds().entrySet()
.stream()
.filter(entry -> validate(entry.getValue(), time))
.map(Map.Entry::getKey)
.findFirst().get()


Thanks

Answer

Here is a similar sample

Assuming a data structure and validate function is defined

val myMap = Map(1 -> "Value1",2 -> "Value2")
val time = System.currentTimeMillis

def validate(str:String, time:Long) = true

Then a straight translate of your code could simply be

myMap.filter(entry => validate(entry._2, time)).map(_._1).take(1)