This question already has answers here:
:: (double colon) operator in Java 8
(17 answers)
Closed 4 years ago.
How does following code work in Akka:
#Override
public Receive createReceive() {
return receiveBuilder()
.match(DeviceManager.RequestTrackDevice.class, this::onTrackDevice)
.match(RequestDeviceList.class, this::onDeviceList)
.match(Terminated.class, this::onTerminated)
.build();
}
onTrackDevice is another method in same class and it takes an input. Here it is invoked without any argument. I understand that passed message would be passed to onTrackDevice too.
But how does it all fit in java syntax?
For most language which support lambda, it could support eta-conversion.
E.g.
x => Math.abs(x) is a lambda, it could be shorted as Math.abs
Java8 also support it, but it did not use Math.abs, it use Math::abs.
So, here this::onDeviceList is just a lambda (it just be supported from java8).
When the actor receive the variable msg with the type RequestDeviceList, it will call this::onDeviceList(msg) internal. Here, as this::onDeviceList is a lambda, so actor can directly call this lambada and set parameter('msg') when call this lambda function (lambda as function parameter, so you did not see parameter here)
In a word, you need to be familiar with java8's lambda support to know how it fits in java syntax.
Related
This question already has answers here:
Multiple lambda method references
(3 answers)
Closed 5 years ago.
list.stream().forEach(e -> method(e)) can be converted to list.stream().forEach(this::method)
Similarly can we convert list.stream().forEach(e -> { method1(e); method2(e);}); using method references expressions. Big apologies if you don't understand question. I am using mobile app first time.
No you cannot.
The point of Method references in Java is to abstract (syntaxically) a lambda expression. Since forEach consumes a function that takes 1 element of type specified by the parent stream, there is no syntax sugar for double application using method references.
Even I'm not sure that this answer is wanted by you,
How about changing the method to static one in that class?
This question already has answers here:
When and why would you use Java's Supplier and Consumer interfaces?
(7 answers)
Closed 6 years ago.
I tried to read on the new java.util.function Consumer, Supplier and Function.
I didn't understand why do we need them, what was the problem and what they solved?
Could you please give me an example of use without those API and with the new API and what is solved?
Perhaps you assume that they have to be more complex than they are.
They are designed to be super simple pieces of code which don't do very much in themselves, but as pieces of code you can pass to a library which can use these pieces of code.
This example prints 100 UUIDs using a Supplier and a Consumer
Stream.generate(UUID::random) // <<< Supplier<UUID>
.limit(100)
.forEach(System.out::println); // <<< Consumer<UUID>
A longer example is
Supplier<UUID> uuidSupplier = UUID::random;
Consumer<UUID> uuidConsumer = System.out::println;
Stream.generate(uuidSupplier)
.limit(100)
.forEach(uuidConsumer);
This question already has answers here:
Java Pass Method as Parameter
(17 answers)
How to pass a function as a parameter in Java? [duplicate]
(8 answers)
Closed 7 years ago.
Is there a way of passing a function to another function and then executing it?
functionCaller(functionToBeCalled());
In java 8 you can use a method reference or lambda
functionCaller(this::functionToBeCalled);
or
functionCaller(() -> functionToBeCalled());
I don't know if I understand very well your question, but effectively you can call a function in param of another function.
You can do this (I suppose your current language is Java):
// if write(...) and getValue() are static method of Writer class
Writer.write(getValue());
// if write(...) and getValue() can just be used by instanciate an object
Writer writer = new Writer();
String val = writer.getValue();
writer.write(val);
There are basic Java programming lesson.
Thanks
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
How to return multiple objects from a Java method?
(25 answers)
Closed 9 years ago.
I'm a new comer form C#, and I know clearly that "Java is always pass-by-value."
But pass-by-reference is useful when we want to get multiple outputs from one method.
How can we get multiple outputs from one method in java, as in C#.
I know one way to do this -- use a generic wrapper class, and get value from the field.
class Wrapper<T> {
public Wrapper(T value) {
Value = value;
}
public T Value;
}
Is there another way to realize this effect?
No, Java does not have out parameters. You can pass an object reference that the method is to modify to pretend that it has out parameters, but this isn't usually the best design and runs into other issues (multithreading and mutable state for one).
The best way to achieve a method that returns multiple values is to have the method return a type that contains multiple values.
Another way to simulate call by reference in Java is to pass a one-element array as a parameter.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can I pass an array as arguments to a method with variable arguments in Java?
What is … in a method signature
I first saw this when I was modding Minecraft. It had a constructor that specified (String ... line), and thought it was just some shorthand that Mojang had created. But now, I was looking over ProcessBuider, and saw it again. I was wondering what this is used for. My best guess is that it allows developers to add as many of that type of object as they want. But if that's the case, why not just use an Array or List?
So, really, I am asking two questions:
What is the "..." operator, and
Why would it be more useful than using an Array or List?
... indicates a multiple argument list to a variadic function: a function that can take a variable number of arguments.
For an example of this, look at PrintStream.format. The first (required) argument is a format String, and the remaining 0 or more arguments fulfill that format.
It is called varargs, and as you say it is used to be able to let a method be called with any number of arguments of the specified type. It was introduced in Java 5.
You can read more in the Java tutorials - Varargs.
This is equivalent to a String[] line. It is Java's equivalent to the varargs keyword in C/C++. Similar to C/C++ it must appear as the last parameter.
You've already answered question #1 yourself. As to why it's more useful, it's just a shorthand that requires less typing.
To answer your second question, one advantage of varargs is that you can call a function taking varargs parameter without passing that param. Whereas instead if your function takes in an array, and you need to call it without any value, the caller needs to explicitly pass null.