replace lamda with method reference for contains [duplicate] - java

This question already has answers here:
Sonar : Replace this lambda with a method reference
(4 answers)
Closed 4 years ago.
I actually wanted to replace this to method reference since sonar is throwing an issue ,I have also searched answers for similar tittle i was't able to find the solution.
String type="test2"
List<String> validSimtSwType = Arrays.asList("test1", "test2", "test3", "test4");
if((validSimtSwType.stream().anyMatch(name -> type.contains(name)))){
//statements
}

You can replace it with a method reference referencing the contains method of the type instance:
if((validSimtSwType.stream().anyMatch(type::contains)))

Related

Null pointer exception inside Optional.ofNullable() [duplicate]

This question already has answers here:
How to convert Optional<Object> to Optional<String> [closed]
(3 answers)
Closed 2 years ago.
I am trying to convert a set to string inside Optional.OfNullable method like:
test.setAbc(Optional.ofNullable(rule.getSampleSet().toString()).orElse(null));
but if sampleSet is null it will give me a NullPointerException.
can anyone tell me how to resolve this issue using .map method with Optional?
I know one traditional way of doing it by checking nullability beforehand:
if(rule.getSampeSet != null)
but I am much interested in knowing if we can do it in one line.
Instead of calling toString() inside the ofNullable, you could map the optional to it:
test.setAbc(Optional.ofNullable(rule.getSampleSet()).map(Object::toString).orElse(null));

How to make a chained object null-safe for iteration [duplicate]

This question already has answers here:
Check chains of "get" calls for null
(11 answers)
Null check chain vs catching NullPointerException
(19 answers)
Closed 4 years ago.
New to Java.
I'm trying to avoid NullPointerException when iterating over a list. CollectionUtils.emptyIfNull() helps, but not for a chained object.
For example:
for (MyObject myObj : CollectionUtils.emptyIfNull(
result.item.getMyObject().getObjects())) {
// do stuff
}
I have no control over MyObject because I wrote a client that consumes this as a dependency. Any of these attributes could be null, and emptyIfNull() only protects against getObjects() returning null.
Is there a way to apply emptyIfNull() or something similar to the entire chain of calls?
Thanks for the help!

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

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?

Store a method's name in a variable and call that method using that variable [duplicate]

This question already has answers here:
How do I invoke a Java method when given the method name as a string?
(23 answers)
Closed 6 years ago.
I want to call a method whose name (with or without parentheses and parameters) is stored in a string variable and call this method using this variable.
Until now I have found that in Java if you do this:
String str = "func();";
System.out.println(str);
This would call method func(). But I don't think this is recommended.
So, my question is:
Is it okay to do this?
Is there any other way to do this?
Thanks.
Method m = YourClass.class.getMethod("methodName", parametersOfYourMethods);

Calling method from a component by its name in a string [duplicate]

This question already has answers here:
How do I invoke a Java method when given the method name as a string?
(23 answers)
Closed 10 years ago.
I just want to know if there is any way to call a method from on an object by its name in a String.
Something like this
setLocation(int,int) it's a method from jLabel1, how can i call this
method(setLocation()) by using its name in string ?
String component = jLabel1.getName();
component.setLocation(x,y);
Sure, you can use the Java reflection API
This SO question may helps you.

Categories

Resources