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)
Related
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:
Java ArrayList to Kotlin Array
(4 answers)
Closed 1 year ago.
So, I try to make a line chart using AAChartModel. I have ArrayList in my code. But in AAchartModel they used an arrayOf instead of ArrayList.
So My Question is, how do I convert from ArrayList to arrayOf ?
arrayOf() returns just an Array. So you can call arrayList.toTypedArray()
This question already has answers here:
Idiomatic way to create a Stream from a Nullable object
(7 answers)
Closed 4 years ago.
The following code gives me a NullPointerException. As my examine, it is caused by containerModels being null.
List<DoseDetailMutableDTOToBaseDoseDetailAdapter> adapters =
containerModels.stream()
.map(DoseDetailMutableDTOToBaseDoseDetailAdapter::new)
.collect(Collectors.toList());
How to fix it using java 8?
This could do the trick:
Optional.ofNullable(containerModels)
.orElse(Collections.emptyList())
.stream()
...
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?
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");