用 ifPresent 和 orElse 替换 isPresent
Replacing isPresent with ifPresent and orElse
我的方法中有以下逻辑,我在其中检查可选参数的值,并根据它构建另一个对象。
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);
});
如何用 ifPresent 和 orElse 替换上面的内容?我正在使用 Java 8,因此 ifPresentOrElse 方法不可用。如果我要将 ifPresent 和 orElse 与匿名内部函数分开使用,我该怎么做?
如有任何帮助,我们将不胜感激。
您可以流过 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)); // did you mean 'record'?
但老实说,它并没有太大变化 - 您的实施看起来可能不那么“java 八分之一”,但还不错 :)
您既不需要 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()
。
我的方法中有以下逻辑,我在其中检查可选参数的值,并根据它构建另一个对象。
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);
});
如何用 ifPresent 和 orElse 替换上面的内容?我正在使用 Java 8,因此 ifPresentOrElse 方法不可用。如果我要将 ifPresent 和 orElse 与匿名内部函数分开使用,我该怎么做?
如有任何帮助,我们将不胜感激。
您可以流过 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)); // did you mean 'record'?
但老实说,它并没有太大变化 - 您的实施看起来可能不那么“java 八分之一”,但还不错 :)
您既不需要 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()
。