Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services 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
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Passing a Method as an Argument

 
Ranch Hand
Posts: 53
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a Coding problem from an Online IDE :

Given a string, reverse defined segments and return the new string.

EG : Given Input s1 = "abcd\nefgh\nijkl\nmnop", Return output : s2 = "dcba\nhgfe\nlkji\nponm"

The Question itself is easy, but I cant figure out the default code in the IDE :



And the test case for this code :



I cant understand the lines :

or


Why would anyone try to pass the method to be tested, along with the parameter that the same Method is going to use for testing to a second completely unrelated method.
 
Marshal
Posts: 66980
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method reference is one way to pass a λ expression. Are you not familiar with method references? If you go through the Java™ Tutorials, you will find out that you are not passing the method (as you might in C), but that is part of the way you can define an object that uses that method.

Adding discussion to what used to be the Java8 forum.
 
Kevin Mckeon
Ranch Hand
Posts: 53
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The method reference is one way to pass a λ expression. Are you not familiar with method references? If you go through the Java™ Tutorials, you will find out that you are not passing the method (as you might in C), but that is part of the way you can define an object that uses that method.

Adding discussion to what used to be the Java8 forum.



Is there a way of doing this without using λ expression ?

Ive tried to pass/call the method by :




And by changing the method Declaration :









But No luck
 
Bartender
Posts: 2227
62
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can pass an instance of an object implementing some interface (Function, UnaryOperator).
 
Campbell Ritchie
Marshal
Posts: 66980
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kevin Mckeon wrote:. . . Is there a way of doing this without using λ expression ? . . .

Most probably yes. Let's start with something simpler that I an actually understand
I presume you have worked out how a method reference is a sort of cut‑down λ from the Java™ Tutorials link I gave you yesterday.

Let's imagine you have a λ all of what it does being to call a method. Let's make it a one‑parameter method add(int) returning an int.Pretty useless except that you can easily understand the problem. Let's have a method foo() taking an IntUnaryOperator as a parameter. The old way you could have created such an object would be with an anonymous class (see the same Java™ Tutorials section):-As you will have seeen from the link, and you could have guessed because it is in the java.base/java.util.function package, IntUnaryOperator is a functional interface, so you can reduce that anonymous class to a λ:-I presume you are familiar with the process of converting an anonymous class to a λ; I have written about it several times before. All you are doing in that λ is calling a method. If you are using a Stream<Integer> or an IntStream, that will provide the argument i, and you will have a reference to myUslessInstance already, which constitutes the first argument. (If that reference is a local variable/method parameter, it must be effectively final.) So all you are doing is to pass the i to a method and collect the result. You do that by retaining the two unknown parts of the λ, namely the object the method is called on and the method name: myUslessInstance::add. No () and the dot is changed to :: and Bob's your uncle.
 
No. No. No. No. Changed my mind. Wanna come down. To see this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!