All Questions
Tagged with option-type java
1,110
questions
-2
votes
0
answers
37
views
Why Java shows strange characters when print a OptionalDouble? [duplicate]
I declared a OptionalDouble vector, and when the program print this give me strange characters instead of the vector values:
[[Ljava.util.OptionalDouble;@37bba4006. [[Ljava.util.OptionalDouble;@...
0
votes
1
answer
92
views
Optional's orElse() executed even when object is present [duplicate]
I've encountered an unexpected behavior while using the Optional API in Java.
Session session = sessionService
.getSessionById(id)
.orElse(
...
1
vote
0
answers
22
views
Why Optional lambda replace with method reference Causing NullPointerException [duplicate]
There is a A Class
class A {
private String str;
public void setStr(String str) {
this.str = str;
}
}
this method works fine;
public static void main(String[] args) {
...
-1
votes
1
answer
122
views
Generate a type from a Java DTO type with many fields being null and have all getters as Optional<T>
I am currently working with a Java DTO class (written by others) that has almost 300 fields where many of these end up being null. What I see in the codebase are lots of null checks and more recently ...
0
votes
3
answers
71
views
Springboot: How to get an entity optional property and check null?
I have a service in Springboot and I want to get an optional entity and to check if is not null to do something after.
User repository class
public interface UserRepository extends JpaRepository<...
2
votes
2
answers
116
views
Optional.ifPresent throws NPE when action is null AND optional not present
Why does this throw a NullPointerException?
The docs for Java 8 state that:
"If a value is present, invoke the specified consumer with the value, otherwise do nothing."
public static ...
2
votes
2
answers
82
views
Monad Map with two values in Java Functional Programming
Forgive me if this is a basic question in FP. Let's consider Optional monad. I know I can use map to transform an Optional to another Optional based on a function (which will be called if optional has ...
0
votes
1
answer
56
views
Optional and complete method
How to work correctly with Optional if I need to complete a method (with some actions, log for example) in case of an empty Optional? Otherwise, get the object from Optional and continue working
I ...
5
votes
1
answer
95
views
In Frege how can I convert an String safely to a Maybe Int?
In Frege I want to convert an String to an Int, but need to handle unparsable strings too.
So I guess I'm looking for something like a readMaybe function. Where do I find this? Or how can I use Java's ...
0
votes
1
answer
122
views
Is there a recommended idiomatic way to adapt a Java Optional to a Kotlin nullable?
I am writing Kotlin code that uses a Java library which uses Optionals, and I would prefer to use Kotlin's nullable idiom in Kotlin-world.
For example, my library may have a function
Optional<Foo&...
0
votes
3
answers
118
views
Cannot Stream average() undefined for OptionalDouble
I'm using this code but eclipse shows an error saying
stream() is undefined for the type OptionalDouble
The error shown by eclipse is
The method stream() is undefined for the type OptionalDouble .
...
1
vote
2
answers
160
views
Chaining Optional and Stream in Java(preferably 11)
I have a list of Foo objects, where each Foo contains a name and a List<Strings>.
I am trying to
find if the fooList contains a Foo object with a target name, and if such element exists,
find ...
0
votes
0
answers
108
views
Does using java.util.Optional affect memory/speed and how does JIT Compiler handle it?
I've been using java.util.Optional extensively in my code for better null handling. I even went as far as creating new "safe" datastructures with stuff like:
public Optional<Integer> ...
1
vote
3
answers
263
views
guard/if let in Java - declare a property or an object if it can be unwrapped
In swift there is the guard let / if let pattern allowing us to declare an object or a property only if it can be unwrapped.
it works a follow:
func getMeaningOfLife() -> Int? {
42
}
func ...
0
votes
3
answers
112
views
How best to populate a variable using Optional?
I'm using an optional ID here but I don't like having to declare TWO variables to receive the data. What is a more concise way to accomplish this without having to declare an extra OptionalOrg ...