All Questions
Tagged with option-type lambda
129
questions
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) {
...
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 ...
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
2
answers
135
views
Can this code be reduced using Java 8 Streams?
I want to use Java 8 lambdas and streams to reduce the amount of code in the following method that produces an Optional. Is it possible to achieve?
My code:
protected Optional<String> ...
2
votes
2
answers
74
views
Is there a way to return an optional value to the calling method from the ifPresent method?
I try to make a code more compact by coding a helper method, because the code is very repetitive.
The code consists of:
call a command (remote ssh, or local command, this part is not interesting for ...
2
votes
1
answer
52
views
How do I add different variables that are present in different classes using .map()/.filters() [closed]
I have three different type of fee (cost, deliveryServiceFee and fee) in different objects and I want to add them up and put them in a variable called "totalDeliveryFee".
Here are the ...
0
votes
1
answer
139
views
Interview question - How to use Optional.of() or Stream.of() to reduce code
I recently made it through to the final round of interview.
At one point in the interview, they asked me to demonstrate my Java 8 knowledge on the following piece of code. They asked me to reduce the ...
0
votes
1
answer
127
views
Custom collector not compile
I'm creating a custom Collector but it is failing when using it while compiling.
private static <T> Collector<T, ?, T> getFirstElement() {
return Collectors.collectingAndThen(
...
1
vote
2
answers
3k
views
Bad return type in lambda expression when using Java's Optional.or() with subclasses
I am trying to use Optional.or to get an object of subclass A or, if empty, an object of subclass B:
interface Node {}
class InnerNode implements Node {}
class LeafNode implements Node {}
Optional<...
0
votes
2
answers
312
views
How to throw an exception in lambdas operating on methods which returns optional
I wrote some lambda expression which adds some values to the map based on methods which return optionals. I would like to throw some specyfic exception when there is no value under key "A". ...
3
votes
1
answer
3k
views
Why does the `orElseThrow()` method in Java take a `Supplier` as a parameter instead of an `Exception`?
Why does the orElseThrow() method in Java take a Supplier as a parameter instead of an Exception? This is the question and the best answer I have found is in here, stating
The overarching concept ...
1
vote
1
answer
138
views
Why Consumer or Supplier argument needs {} for multiple annotations [duplicate]
Let's say there's an Optional optString. Now if I use it as below:
optString.ifPresent({
//some statements
}, () -> throw new Exception());`, it fails to compile.
Why do I've to wrap the ...
1
vote
0
answers
1k
views
Stream Optional List and return Optional List (JAVA 16)
I have an Optional List
Optional<List<ProductType>> relatedProductTypes;
I want to populate this list, retrieving them from the Object Category:
public class Category {
private String ...
-1
votes
4
answers
425
views
Lambda functions with Optional instead of nested if else [closed]
Optional<String> myData;
Set<String> mySet;
if(myData.isPresent()) {
if(myData.get().contains(“testValue”)) {
mySet.add(“somedata”);
}
}
if(!myData.isPresent()) {
mySet....
0
votes
1
answer
147
views
JAVA8 Optional and Lambdas
Suppose I have this class model hierarchy:
public class A {
private Integer id;
private List<B> b;
}
And:
public class B {
private Integer id;
private List<C> c;
}
And finally:
...