Python Java forEach equivalent -


i java programmer. started learning python few days ago. i'm wondering: there equivalents


map.foreach(system.out::println)


in python lambdas? or loop:


for e in map: print(e)


there no equivalent java's imperative iterable.foreach or stream.foreach method. there's map function, analogous java's stream.map, it's applying transformations iterable. java's stream.map, doesn't apply function until perform terminal operation on return value.

you abuse map job of foreach:

list(map(print, iterable)) 

but it's bad idea, producing side effects function shouldn't have , building giant list don't need. it'd doing

somelist.stream().map(x -> {system.out.println(x); return x;}).collect(collectors.tolist()) 

in java.

the standard way in python loop:

for thing in iterable:     print(thing) 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -