This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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: 12862
215
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: 62293
194
  • 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: 21560
100
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: 62293
194
  • 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: 62293
194
  • 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.
 
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!