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);
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:
Why is order of expressions in if statement important
(9 answers)
short-circuiting behavior of conditional OR operator(||)
(3 answers)
Closed 3 years ago.
I have stumbled upon this odd behaviour while working with jasper soft reports and I would like to have it explained since I don't know much about Java.
Basically, I have a print when expression:
($P{Parameter_name}!=0 || $P{Parameter_name}==null)
? true : false
-> this returns false
($P{Parameter_name}==null || $P{Parameter_name}!=0)
? true : false
-> this returns true
The only difference is the equation order but logically it shouldn't make a difference.
The parameter is null.
I know that for example SQL wouldn't care for the order and would always evaluate true with a simple or statement like this.
If the parameter is null, you will get a NullPointerException when evalulating
$P{Parameter_name}!=0
And maybe this exception will prevent Jasper from checking the second condition.
In your second version, the test
$P{Parameter_name} == null
will be evaluated to true, which means that
$P{Parameter_name} != 0
won't be evaluated at all (and no NPE will be raised)
This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 4 years ago.
capacity = (cap > 0) ? cap : CAPACITY;
I'm just looking through my lecture notes, and I can't figure out what this line of code does. Can someone help me?
It's called conditional expression and in your case it means that if cap is greater than 0, the capacity variable will get the value "cap", if false then it will get the value "CAPACITY".
This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 9 years ago.
In Java, setting a variable to a value based on a condition could be done in one line
like so:
variable = (!true) ? 1 : 2
This would result to '2'.
Is there python equivalent to this code?
Thank you.
variable = 1 if not True else 2
General ternary syntax:
<value_if_true> if <condition> else <value_if_false>
This is called a conditional expression in Python, and is mostly equivalent to the "ternary operator" in C-family languages (although it's not actually an operator). The original proposal PEP 308 has more details.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java Operators : |= bitwise OR and assign example
boolean bAlive;
bAlive |= this.properties.containsKey(name);
In the above , the code uses '|'.
Why using '|' ?
Thanks in advance.
The boolean is being OR'ed with the value on the right.
If this.properties.containsKey(name) is TRUE, then bAlive is set to TRUE.
Otherwise, bAlive remains the same.