Java 8 method references for multiple statements in streams [duplicate] - java

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?

Related

Why and how does this strange method return type compile? [duplicate]

This question already has answers here:
Strange array return type
(6 answers)
Java function definition with brackets syntax [duplicate]
(1 answer)
Weird "[]" after Java method signature
(4 answers)
Closed 3 years ago.
The following method has a return type of int[][]. Note how one of the []'s are on the right hand side of the ()'s.
Why does this work and what is this behavior called?
int[] numbers () [] {
return null;
}
From the JLS
The declaration of a method that returns an array is allowed to place some or all of the bracket pairs that denote the array type after the formal parameter list. This syntax is supported for compatibility with early versions of the Java programming language. It is very strongly recommended that this syntax is not used in new code.

Calling a method from createReceive in Akka [duplicate]

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.

Create class with mathematic operations [duplicate]

This question already has answers here:
"+" operator for Java-classes
(5 answers)
Closed 5 years ago.
I'm trying to make a java library with a bunch of extra classes, and I was adding one for Imaginary numbers. Is there any way in java to make a custom class that is affected by mathematics operations
for example
Imaginary(10) * Imaginary(50) = "500i"
No, it is not possible to define custom arithmetic operators that overload default operators, but you can create methods like Imaginary.mutiply(Imaginary i).
You cannot overload operators in Java. See this previous answer to a similar question: https://stackoverflow.com/a/5883909/1701316
Your class will need to implement its operations as methods. If you'd like, since any character is allowed in a method name, you can name them with the typical operators, but they'll still need to be called with dot-notation: Imaginary(10).*(Imaginary(50))

What is the different between Method and Function in JAVA? [duplicate]

This question already has answers here:
What's the difference between a method and a function?
(41 answers)
Closed 7 years ago.
I was wondering What is the different between Method and Function in JAVA?
it the same, function or methods are just different ways to call it, but in my long experience with java i commonly hear function from my fellow developers the last time i heard anyone say 'method' was back when i still used C++

Passing params in java by reference [duplicate]

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.

Categories

Resources