This question already has answers here:
Why can't I use the ternary ? operator to select between two function calls?
(4 answers)
Issue with The left-hand side of an assignment must be a variable [duplicate]
(3 answers)
Closed 7 years ago.
(items[position] != null) ? et.setText(items[position]) : et.setHint(position==0? "Title" : "Body");
Why i can't use first pattern?
But in this way it works:
if (items[position] != null)
et.setText(items[position]);
else et.setHint(position==0? "Title" : "Body");
Related
This question already has answers here:
Filter values only if not null using lambda in Java8
(6 answers)
Filter null values after map in Java 8 [duplicate]
(1 answer)
Closed 6 months ago.
Let's say I have a Java Stream that contains null values.
How do you remove them ?
Here's a few ways I can think of:
stream.filter(x -> x != null).
stream.filter(Objects::nonNull)
This question already has answers here:
Ternary operator gives "Not a statement" error [duplicate]
(2 answers)
Java Ternary without Assignment
(4 answers)
Closed 1 year ago.
I am trying to make a simple ternary operation on a hash map:
hashMap.get(number) == 1 ?
hashMap.remove(number) :
hashMap.merge(number, 1, Math::subtractExact);
I am getting a 'Not a statement' error from my IDE. What I don't understand is where this is coming from: both calls to remove and merge sound like statements to me.
You have to assign the value of the ternary operator to some variable:
Integer value =
hashMap.get(number) == 1 ?
hashMap.remove(number) :
hashMap.merge(number, 1, Math::subtractExact);
This question already has answers here:
How to check type of variable in Java?
(16 answers)
What is the 'instanceof' operator used for in Java?
(18 answers)
Closed 1 year ago.
I want check the variable type. How do I do that? For e.g
if (num is of String type )
{This must be executed}
I'm currently using Java 17. Any suggestions?
You can use instanceof String to check whether a variable is a String.
if (num instanceof String) {
// code to be executed
}
This question already has answers here:
Can java's assert statement allow you to specify a message?
(4 answers)
Closed 2 years ago.
What is the Java equivalent this?
assert(0 && "description");
I've tried
assert(false && "description");
But Java types are too strict for that. Any tips on the idiomatic way to add a description to an assert statement?
You can add a description to an assertion using:
assert false : "description";
This is described as
assert Expression1 : Expression2 ;
on https://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html
This question already has answers here:
Return value from Optional [closed]
(1 answer)
Java Optional if object is not null - returns the method result, if null - returns default value
(6 answers)
Closed 4 years ago.
I often find myself using this idiom:
AtomicReference<MyCoolObject> coolReference = new AtomicReference(new MyCoolObject());
getOptionalValue().ifPresent(presentValue -> {
coolReference.set(new MyCoolObject(presentValue));
});
return coolReference.get();
Is this a bad habit, or a legitimate use of AtomicReference?