There are three kinds of actuaries: those who can count, and those who can't.
There are three kinds of actuaries: those who can count, and those who can't.
Often the most important part of the news is what they didn't tell.
Stephan van Hulst wrote: To pass the forEach() method an instance of an anonymous class, you have to do this:
,
Stephan van Hulst wrote:. You can't pass it a MyInterface,
Monica Shiralkar wrote:Here I had to convert object to String using toString method then why is is so that in the lambda code above it was not required to convert to string from object ?
Stephan van Hulst wrote:Note that this requires that you've properly parameterized myList, which you currently haven't.
Monica Shiralkar wrote:Ok. So in which case can we pass our own interface to a higher order method ?
Piet Souris wrote:Fair enough, but I was concentrating on the 'MyInterface'. So the full story would then become
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie wrote:It does seem overkill, however, to extend that interface to add a method. What would happen if you added that method to the anonymous class?
There is a lot more to it than that. Have you tried it yourself?Fikri Harlov wrote:. . . If method display() contains 20 lines source code, it will be ugly to be in the forEach() . . .
Campbell Ritchie wrote:Nobody ever liked anonymous classes anyway, . . .
To quote Carey,Mike Simmons wrote:. . . an overstatement.
When I wanted to show how you can convert an anonymous class to a λ or a method reference!But... when was the last time you used an anonymous class for something that couldn't be done more easily with a lambda? . . .
Fikri Harlov wrote:Whenever I need a class with fields rather than a simple function like a lambda.
I found some pretty shells, some sea glass and this lovely tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|