This question already has answers here:
Variable used in lambda expression should be final or effectively final
(9 answers)
Closed 4 years ago.
I have the sample code below
String value="xyz";
dataList.stream().filter(obj -> obj.equals(value))
My question is: how to have the value available in my lambda expression in Filter.
It is directly accessible if you are using Java 8, see the below code.
public static void main(String args[]) {
String value="xyz";
List<String> dataList = new ArrayList<>();
dataList.add("abc");
dataList.add("def");
dataList.add("ghi");
dataList.add("xyz");
dataList.add("jkl");
dataList.add("mno");
dataList.stream().filter(obj -> obj.equals(value)).forEach(System.out::println);
}
Related
This question already has answers here:
Java: When is a static initialization block useful?
(13 answers)
Static Initialization Blocks
(14 answers)
Closed 4 years ago.
Is there any difference in when static class member initialisation is done between the following two situations:
(1)
static ArrayList<String> x = new ArrayList<String>();
(2)
static ArrayList<String> x;
static
{
x = new ArrayList<String>();
}
As far as I understand it these are effectively equivalent and both guarantee that x is initialised once and once only, and before any class method or constructor can modify it.
First approach is less error prone, for example you can have a static block calling x.get(0); which will produce NullPointerException`
This question already has answers here:
Java Pass Method as Parameter
(17 answers)
Closed 4 years ago.
I'm new to Java and I'm confused about actual parameters in Java. Since it's just parameters, I assume that literals, constants,variables, expressions can be actual parameters during function calling.
But I'm confused about a function call. Is it possible to have a function call being an actual parameter in Java?
If your question is "are functions first-class citizens in Java, as they are in C" then the question is "no, not really".
But with Java 8's lambdas, you can get pretty close:
import java.util.function.IntFunction;
public class Main {
public static void main(String[] args) {
final IntFunction square = x -> x * x;
printFunctionResult(square, 2);
}
public static void printFunctionResult(final IntFunction function, final int value) {
System.out.println(function.apply(value));
}
}
This question already has answers here:
How do I pass a primitive data type by reference?
(8 answers)
Closed 4 years ago.
I have the following code
private static void modifyX(int x) {
if (x!=0) {
modifyX(--x);
}
}
I want the value of my variable to be updated after the recursive call, so I wrote the following code:
public static void main(String... args) {
int x = 5;
modifyX(x);
System.out.println("Modified value:\t" + x);
}
However, the value remains the same (5).
Why is my variable not updating?
You are not passing the same instance of the value 5. Instead, the JVM is creating a new int with a value of 5, and passing that to your method.
See this thread for more information.
This question already has answers here:
How to declare a constant in Java?
(3 answers)
Closed 7 years ago.
How can I define a constant in my class that returns a object? What I'm looking for is something like BigInteger.ZERO
This is how BigInteger.ZERO is defined :
public static final BigInteger ZERO = new BigInteger(new int[0], 0);
public class SomeClass {
public static final type CONST_NAME = new SomeObject();
...
}
Then you can do SomeClass.CONST_NAME
This question already has answers here:
public static void main(String arg[ ] ) in java is it fixed?
(9 answers)
Closed 7 years ago.
I have a basic question regarding java.I know it's very basic.However, I want to confirm whether my approach is correct or not.
Generally,we write
public static void main(String[] args)
The compiler starts executing from there on!
I tried it writing in a different way.
class input
{
public static void main(String args)
{
input.main("hello");
System.out.println(args);
}
}
The error i am getting is:
Exception in thread "main" java.lang.NoSuchMethodException:
substrings.main([Ljava.lang.String;)
at java.lang.Class.getMethod(Class.java:1786)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:125)
I have two questions:
What is wrong in having String args instead of String[] args??(Is JVM hardcoded like accepting array arguments?)
What's wrong if i call it as input.main("0") ?
Your main function MUST match the function signature in the main function specification.