• 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
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

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: 12537
206
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: 60867
190
  • 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: 21503
96
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: 60867
190
  • 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: 60867
190
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!