Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

Java 8 learning

 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, guys, i have just completed online course on Java 8. So to test my knowledge i got following question from my colleague:

We have a csv file in which 3 fields are their. 1) osName 2) Software 3) version

So we need to return outdated software version. User will provide the software and version name. File looks like as below:




So, after spending few hours i come up with the solution but its looking very much complicated. Any idea how to improve it further:

 
author & internet detective
Posts: 41849
904
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the first method, you could rewrite it with streams:



The second method doesn't print anything when I run the method so I'm not sure what it is supposed to do. It definitely feels overly complex. Can you describe what you are trying to get it to do?
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given that you have comma separated values, you could use JDBC CSV drivers.
This would mean that you would not need to use streams.

I wonder which would preform better:
Loading a CSV file into a list with streams or Loading a CSV value into a list using JDBC.
 
Jeanne Boyarsky
author & internet detective
Posts: 41849
904
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or a CSV library. Either of which would handle commas in the values.

This felt like an educational/playing thing with streams vs an attempt to deal with CSV in the best way so I didn't say anything.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that this could very well be a good teaching moment Jeanne. Often times there are more than one way to handle programming problems.
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So , i have updated my main method by adding some print statements. Hope it helps to understand what i am getting:



My Software class:



The output looks like as below:


printing file contents
[Software [osName=Linux, softwareName=winscp, version=9.0], Software [osName=Linux, softwareName=winscp, version=10.0], Software [osName=Linux, softwareName=notepad++, version=11.0], Software [osName=Linux, softwareName=java, version=6.0], Software [osName=Ubuntu, softwareName=winscp, version=9.9], Software [osName=Ubuntu, softwareName=notepad++, version=11.1], Software [osName=Ubuntu, softwareName=notepad++, version=10.0], Software [osName=Ubuntu, softwareName=java, version=7.0], Software [osName=Windowa, softwareName=winscp, version=10.1], Software [osName=Windowa, softwareName=notepad++, version=10.0], Software [osName=Windowa, softwareName=java, version=8.0], Software [osName=MAC, softwareName=winscp, version=7.0], Software [osName=MAC, softwareName=notepad++, version=9.0], Software [osName=MAC, softwareName=java , version=10.0]]
------------------------
printing contents of the map grouped by OS

{Ubuntu={java=[Software [osName=Ubuntu, softwareName=java, version=7.0]], winscp=[Software [osName=Ubuntu, softwareName=winscp, version=9.9]], notepad++=[Software [osName=Ubuntu, softwareName=notepad++, version=11.1], Software [osName=Ubuntu, softwareName=notepad++, version=10.0]]},

Linux={java=[Software [osName=Linux, softwareName=java, version=6.0]], winscp=[Software [osName=Linux, softwareName=winscp, version=9.0], Software [osName=Linux, softwareName=winscp, version=10.0]], notepad++=[Software [osName=Linux, softwareName=notepad++, version=11.0]]},

Windowa={java=[Software [osName=Windowa, softwareName=java, version=8.0]], winscp=[Software [osName=Windowa, softwareName=winscp, version=10.1]], notepad++=[Software [osName=Windowa, softwareName=notepad++, version=10.0]]},

MAC={winscp=[Software [osName=MAC, softwareName=winscp, version=7.0]], java =[Software [osName=MAC, softwareName=java , version=10.0]], notepad++=[Software [osName=MAC, softwareName=notepad++, version=9.0]]}}
------------------------
printing contents of the map grouped by software in a OS

[{java=[Software [osName=Ubuntu, softwareName=java, version=7.0]], winscp=[Software [osName=Ubuntu, softwareName=winscp, version=9.9]], notepad++=[Software [osName=Ubuntu, softwareName=notepad++, version=11.1], Software [osName=Ubuntu, softwareName=notepad++, version=10.0]]},

{java=[Software [osName=Linux, softwareName=java, version=6.0]], winscp=[Software [osName=Linux, softwareName=winscp, version=9.0], Software [osName=Linux, softwareName=winscp, version=10.0]], notepad++=[Software [osName=Linux, softwareName=notepad++, version=11.0]]},

{java=[Software [osName=Windowa, softwareName=java, version=8.0]], winscp=[Software [osName=Windowa, softwareName=winscp, version=10.1]], notepad++=[Software [osName=Windowa, softwareName=notepad++, version=10.0]]},

{winscp=[Software [osName=MAC, softwareName=winscp, version=7.0]], java =[Software [osName=MAC, softwareName=java , version=10.0]], notepad++=[Software [osName=MAC, softwareName=notepad++, version=9.0]]}]
------------------------

is there any winscp version older than 9?
Software [osName=MAC, softwareName=winscp, version=7.0]
 
Jeanne Boyarsky
author & internet detective
Posts: 41849
904
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tushar,
Ok. The reason I wasn't getting any output is my Software class didn't have the constructor in the same order so nothing ever matched. Careless mistake I know. But good illustration of why it helps to provide a full example.

Anyway, this outputs the same thing as the final part of your example. So the nested for each's don't need to be there.

 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Tushar,



So simple and i made it overly complicated... aha...

You are genius. anyway it is good learning for developing better coding.. Thanks...
 
Jeanne Boyarsky
author & internet detective
Posts: 41849
904
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:So simple and i made it overly complicated... aha...


. It's a good example of streams though. I had trouble figuring out what yours did. Which was a signal that something was wrong!
 
reply
    Bookmark Topic Watch Topic
  • New Topic