• 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

Modern Java in Action: Where to use Lambdas

 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Currently, I use lambdas to replace anonymous classes and not much else.  What are some patterns to look for in pre-Java 8 code that are good candidates for using lambdas?  What are situations when I'm developing an application where I should try a lambda instead?
 
Marshal
Posts: 79406
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apart from the places you would use an anonymous class, look for functional interfaces. You can use things like an IntBinaryOperator. You might want to writeYou can find all sorts of similar functions in the same package, about thirty of them. You can find interfaces like Comparator which have methods creating Comparators, as was explained in the Stuart Marks video which Rob S posted a few weeks ago.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lambdas can not just replace anonymous inner classes, they can also replace the need for template methods. Instead of creating an abstract base class that has abstract methods that need to be overridden, you can provide lambdas to provide that functionality.

A quick example from some code I've written some time ago; this class has a getter (String -> String), setter ((String, String) -> String) and a remover (String -> ()). Slightly simplified:

Before Java 8, I had to make this class abstract, and provide (anonymous) implementations for using a Map or System properties as a backing mechanism:
 
Ranch Hand
Posts: 122
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A common use case for programs is to search through a collection of data to find items that match specific criteria.......

  Application of Lamda in a combination of streams, in this case, produces clean & elegant and simple solution .. .try it out!!!

 
Enthuware Software Support
Posts: 4829
52
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Lambdas can not just replace anonymous inner classes, they can also replace the need for template methods. Instead of creating an abstract base class that has abstract methods that need to be overridden, you can provide lambdas to provide that functionality.

A quick example from some code I've written some time ago; this class has a getter (String -> String), setter ((String, String) -> String) and a remover (String -> ()). Slightly simplified:

Before Java 8, I had to make this class abstract, and provide (anonymous) implementations for using a Map or System properties as a backing mechanism:



This example does seem to save you quite a bit of code but I am not sure if this is a good approach from a design perspective. You now have one class that does one thing in two different ways. I think there is an anti-pattern lurking in there somewhere Abstract class and two concrete classes looks better to me. Having an interface would probably be even better. Any thoughts?
 
Campbell Ritchie
Marshal
Posts: 79406
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Anilprem wrote:. . . . You now have one class that does one thing in two different ways. . . .

I thought one of those classes was a pre‑λ Java7 version and the other the refactored Java8+ version.
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Paul is referring to the top one with the Map and System functions. The thing is, this Values class is a utility class. It's supposed to be a wrapper around all types of implementations, and it doesn't matter what these implementations do, as long as they follow the contract. It's not very different from sub classing. You may consider it an anti-pattern, I call it a separation of concerns.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:....The thing is, this Values class is a utility class. It's supposed to be a wrapper around all types of implementations, and it doesn't matter what these implementations do ...



I think that it is not a good use-case, one object holds data in memory and allows it to be accessed using key value pair vs another object of the same class simply asks the OS for the value for a given key. These are two different cases. It will be confusing for me. Next, the word "Values" itself does not bring "key" and "value" pair to my mind.

Coming back to the original discussion by Knute, other than replacing anonymous classes, one good idea would be to modify methods to accept parameters from function package. e.g. if a method is supposed to return a list of employees, It can have an "Predicate<Employee> filter" as a parameter.

 
We can fix it! We just need some baling wire, some WD-40, a bit of duct tape and this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic