Logo

Raúl Raja

  • Archive
  • RSS
  • Ask me anything

Scala vs Java manipulating collections

One of the nicest scala features is for comprehension loops. They allow you to express the expected outcome of the loop and manipulate Seq, List and other collections in a way that saves time and removes clutter from its Java counterpart.

Consider the following…

https://gist.github.com/raulraja/4971984
The Java code is just straightforward iterate over the collection, turn its values to ints and then check if the int is even before we add it to a new collection. Both of the scala code result in the same result a List[Int] of even numbers. Depends on your style or the logic involved you can take several approaches.
  1. Go for the yield style where as you iterate over a collection with a for comprehension and a guard the yield sends new elements to the resulting List.
  2. Use the map and filter FP style approach where you map the collection to a new collection of Ints that is then filtered and yields a new collection of even ints.
  3. Check out some other ways to do the same task more efficiently in scala as well contributed by some members on the G+ Scala community
    • #Scala
    • #java
  • 3 months ago
  • Comments
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

Recent comments

Blog comments powered by Disqus
← Previous • Next →

About

Co-Founder and Dev @47deg

Me, Elsewhere

  • @raulraja on Twitter
  • Facebook Profile
  • raulraja on Flickr
  • raulraja on Foursquare
  • Linkedin Profile
  • raulraja on github

Twitter

loading tweets…

I Dig These Posts

See more →
  • Post via javielinux
    5 consejos para crear tus estilos en Android

    A continuación dejo algunos consejos a la hora de crear estilos y temas en Android. Todos estos...

    Post via javielinux
  • Video via jorgegalindo
    Video

    Ayer me puse tierno y posteé un video de como utilizar la tecnología para planear tener un bebe… Pues pilla! Como utilizar Google Glasses para ligar...

    Video via jorgegalindo
  • RSS
  • Random
  • Archive
  • Ask me anything
  • Mobile

Copyright Raúl Raja 2013.

Effector Theme by Pixel Union