• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Java 8 Functional Question  RSS feed

 
Ranch Hand
Posts: 179
13
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm looking for a way to rewrite the following code using Java 8's functional programming capabilities.

I have a method that returns a list of objects from a database table.  If the list is empty then I return a list of objects from a second, different database table.  Currently this is how I'm handling this logic:



Can this be accomplished using streams?
 
Sheriff
Posts: 13374
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this article will give you some ideas: https://dzone.com/articles/optional-ispresent-is-bad-for-you
 
Marshal
Posts: 63793
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Simon Ritchie wrote:. . . Can this be accomplished using streams?

Yes, it probably can, but why? Don't go trying to specify an implementation detail at such an early stage in the process.
 
Sheriff
Posts: 21689
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Maybe this article will give you some ideas: https://dzone.com/articles/optional-ispresent-is-bad-for-you


Optional will not help here, because its definition of empty for object is null - not empty lists.
 
Simon Ritchie
Ranch Hand
Posts: 179
13
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Simon Ritchie wrote:. . . Can this be accomplished using streams?

Yes, it probably can, but why? Don't go trying to specify an implementation detail at such an early stage in the process.



Thanks Campbell, I hadn't seen that page before.  I'm trying to use streams and functional programming as much as possible in my development as I'm trying to get up to speed with the technology and using if statements where I work seems to be looked down on.  It's not that they're forbidden but often when code is reviewed I'm encouraged to find a more functional programming solution to conditionality.
 
Campbell Ritchie
Marshal
Posts: 63793
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and if you get that to run, I shall be very surprised
 
Campbell Ritchie
Marshal
Posts: 63793
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I trust the code will be familiar to you, and that Stream#max() returns an Optional<T>. That is where the orElse() method comes from.
 
It's exactly the same and completely different as this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!