用 ifPresent 和 orElse 替换 isPresent

huangapple go评论97阅读模式
英文:

Replacing isPresent with ifPresent and orElse

问题

  1. AtomicReference<Employee> employeeValue = null;
  2. questions.forEach(question -> {
  3. employeeValue = question.isBoolean().isPresent()
  4. ? new AtomicReference<>(Employee.builder()
  5. .withBooleanValue(Boolean.valueOf(question.value()))
  6. .build())
  7. : new AtomicReference<>(Employee.builder()
  8. .withStringValue(question.value())
  9. .build());
  10. Record record = Record.builder()
  11. .withId(question.id())
  12. .withValue(employeeValue.get())
  13. .build();
  14. answers.add(record);
  15. });

如果我使用 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.

  1. AtomicReference&lt;Employee&gt; employeeValue = null;
  2. questions.forEach(question -&gt; {
  3. if(question.isBoolean().isPresent()) {
  4. employeeValue.set(Employee.builder()
  5. .withBooleanValue(Boolean.valueOf(question.value()))
  6. .build());
  7. } else {
  8. employeeValue.set(Employee.builder()
  9. .withStringValue(question.value())
  10. .build());
  11. }
  12. Record record = Record.builder()
  13. .withId(question.id())
  14. .withValue(employeeValue.get())
  15. .build();
  16. answers.add(record);
  17. });

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(如问题中所示)。我相信这样做可以:

  1. questions.forEach(question -> {
  2. Employee empl = question.isBoolean()
  3. .map(b -> Employee.builder()
  4. .withBooleanValue(Boolean.valueOf(question.value()))
  5. .build())
  6. .orElseGet(() -> Employee.builder()
  7. .withStringValue(question.value())
  8. .build());
  9. Record record = Record.builder()
  10. .withId(question.id())
  11. .withValue(empl)
  12. .build();
  13. answers.add(record);
  14. });

如果你愿意,你可能可以将这个思想应用到其他答案中的流内部。与使用 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:

  1. questions.forEach(question -&gt; {
  2. Employee empl = question.isBoolean()
  3. .map(b -&gt; Employee.builder()
  4. .withBooleanValue(Boolean.valueOf(question.value()))
  5. .build())
  6. .orElseGet(() -&gt; Employee.builder()
  7. .withStringValue(question.value())
  8. .build());
  9. Record record = Record.builder()
  10. .withId(question.id())
  11. .withValue(empl)
  12. .build();
  13. answers.add(record);
  14. });

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,并使用peekmap-orElse结构来实现相同的结果:

  1. questions.stream()
  2. .peek(question -> {
  3. Employee employee = question.isBoolean()
  4. .map(b -> Employee.builder().withBooleanValue(Boolean.valueOf(question.value())).build())
  5. .orElse(Employee.builder().withStringValue(question.value()).build());
  6. employeeValue.set(employee);
  7. })
  8. .map(question -> Record.builder().withId(question.id()).withValue(employeeValue.get()).build())
  9. .forEach(answers.add(answer)); // 你是不是指的是'record'?

但说实话,这并没有改变太多 - 你的实现可能看起来不太像“Java八”的风格,但是已经很好了 用 ifPresent 和 orElse 替换 isPresent

英文:

You can stream through questions and use peek and map-orElse construction to achieve the same result:

  1. questions.stream()
  2. .peek(question -&gt; {
  3. Employee employee = question.isBoolean()
  4. .map(b -&gt; Employee.builder().withBooleanValue(Boolean.valueOf(question.value())).build())
  5. .orElse(Employee.builder().withStringValue(question.value()).build());
  6. employeeValue.set(employee);
  7. }
  8. )
  9. .map(question -&gt; Record.builder().withId(question.id()).withValue(employeeValue.get()).build())
  10. .forEach(answers.add(answer)); // did you mean &#39;record&#39;?

But to be honest it does not change a lot - your implementation looks maybe less "java eightish" but is fine 用 ifPresent 和 orElse 替换 isPresent

huangapple
  • 本文由 发表于 2020年9月9日 07:40:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63802894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定