• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

List to Map

 
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. I'm learning Scala and came across this post https://coderanch.com/t/640317/java/java/List-Map
I wonder how it can be done in Scala. How would you do it?

2. Mine:
 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My version would be similar to yours i.e. using the standard collections in Scala, but with a for-comprehension for the second step.
The results are the same as doing it your way:

I think 1 or 2 lines of code is pretty concise for this kind of thing.
[edit: Fixed typo on "v.map(_.aid)". Doh!]
 
H Paul
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Long Live Scala!

- I notice most of the code is the form



and not with .methodName



- With IDE b) style is helpful where as a) style is for some one who know by heart all the method.
 
H Paul
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With my code:

Is a second reading/interpration correct? the variable mapEntry should be tuple2 (scale.TupleN)

empList groupBy (_.employeeId) map ( mapEntry => (mapEntry._1, mapEntry._2 map (_.addressId )))

should be re-written (using scale.TupleN given the expression tuple2._1 retrieve the 1st item)

empList groupBy (_.employeeId) map ( tuple2 => (tuple2._1, tuple2._2 map (_.addressId )))
 
chris webster
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I think a Map entry is just a (key, value) tuple anyway, so all you've done is change your local name for what was already a tuple. Scala knows it's a tuple, so you're probably better off using a more meaningful name.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic