0

I am new to scala I am trying to understand the streams. Can I some tell me the difference between java 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

smac89
  • 39,374
  • 15
  • 132
  • 179
Newbie
  • 691
  • 4
  • 16
  • 28
  • I don't believe itemList.getIds() is a list. More likely a Map. You don't need the scala stream class for this. In scala all the functional operations like filter, map are on every collection including Map. – Lionel Port Sep 22 '16 at 22:31
  • @LionelPort Yeah I am sorry, it is a map. So I can straight way use filter ? – Newbie Sep 22 '16 at 23:40

1 Answers1

0

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)
Lionel Port
  • 3,492
  • 23
  • 26