英文:
Replacing isPresent with ifPresent and orElse
问题
AtomicReference<Employee> employeeValue = null;
questions.forEach(question -> {
employeeValue = question.isBoolean().isPresent()
? new AtomicReference<>(Employee.builder()
.withBooleanValue(Boolean.valueOf(question.value()))
.build())
: new AtomicReference<>(Employee.builder()
.withStringValue(question.value())
.build());
Record record = Record.builder()
.withId(question.id())
.withValue(employeeValue.get())
.build();
answers.add(record);
});
如果我使用 ifPresent 和 orElse 分别与匿名内部函数分开使用,应该如何操作?
希望能对您有所帮助。
英文:
I have the following logic in my method where I check for the value of an optional parameter, and depending on that I build another object.
AtomicReference<Employee> employeeValue = null;
questions.forEach(question -> {
if(question.isBoolean().isPresent()) {
employeeValue.set(Employee.builder()
.withBooleanValue(Boolean.valueOf(question.value()))
.build());
} else {
employeeValue.set(Employee.builder()
.withStringValue(question.value())
.build());
}
Record record = Record.builder()
.withId(question.id())
.withValue(employeeValue.get())
.build();
answers.add(record);
});
How can I replace the above with ifPresent and orElse? I'm using Java 8 and therefore ifPresentOrElse method is not available. If I am to use ifPresent and orElse separately with anonymous inner function, how do I go about it?
Any help would be much appreciated.
答案1
得分: 2
你既不需要 isPresent()
也不需要 ifPresent()
。你不需要 peek()
(如其他答案中所示),也不需要 AtomicReference
(如问题中所示)。我相信这样做可以:
questions.forEach(question -> {
Employee empl = question.isBoolean()
.map(b -> Employee.builder()
.withBooleanValue(Boolean.valueOf(question.value()))
.build())
.orElseGet(() -> Employee.builder()
.withStringValue(question.value())
.build());
Record record = Record.builder()
.withId(question.id())
.withValue(empl)
.build();
answers.add(record);
});
如果你愿意,你可能可以将这个思想应用到其他答案中的流内部。与使用 Stream.forEach()
不同,我更喜欢收集到一个集合中,比如一个列表,然后使用 answers.addAll()
。
英文:
You neither need isPresent()
nor ifPresent()
. You don’t need peek()
(as in the other answer) nor an AtomicReference
(as in the question). I believe that this does it:
questions.forEach(question -> {
Employee empl = question.isBoolean()
.map(b -> Employee.builder()
.withBooleanValue(Boolean.valueOf(question.value()))
.build())
.orElseGet(() -> Employee.builder()
.withStringValue(question.value())
.build());
Record record = Record.builder()
.withId(question.id())
.withValue(empl)
.build();
answers.add(record);
});
You can probably apply this idea inside the stream from the other answer if you want. Rather than using Stream.forEach()
I’d prefer to collect into a collection like a list and then use answers.addAll()
.
答案2
得分: 1
你可以通过流式处理questions
,并使用peek
和map
-orElse
结构来实现相同的结果:
questions.stream()
.peek(question -> {
Employee employee = question.isBoolean()
.map(b -> Employee.builder().withBooleanValue(Boolean.valueOf(question.value())).build())
.orElse(Employee.builder().withStringValue(question.value()).build());
employeeValue.set(employee);
})
.map(question -> Record.builder().withId(question.id()).withValue(employeeValue.get()).build())
.forEach(answers.add(answer)); // 你是不是指的是'record'?
但说实话,这并没有改变太多 - 你的实现可能看起来不太像“Java八”的风格,但是已经很好了
英文:
You can stream through questions
and use peek
and map
-orElse
construction to achieve the same result:
questions.stream()
.peek(question -> {
Employee employee = question.isBoolean()
.map(b -> Employee.builder().withBooleanValue(Boolean.valueOf(question.value())).build())
.orElse(Employee.builder().withStringValue(question.value()).build());
employeeValue.set(employee);
}
)
.map(question -> Record.builder().withId(question.id()).withValue(employeeValue.get()).build())
.forEach(answers.add(answer)); // did you mean 'record'?
But to be honest it does not change a lot - your implementation looks maybe less "java eightish" but is fine
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论