用 ifPresent 和 orElse 替换 isPresent

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

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&lt;Employee&gt; employeeValue = null;
    questions.forEach(question -&gt; {
        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 -&gt; {
		Employee empl = question.isBoolean()
				.map(b -&gt; Employee.builder()
	                    .withBooleanValue(Boolean.valueOf(question.value()))
	                    .build())
				.orElseGet(() -&gt; 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,并使用peekmap-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八”的风格,但是已经很好了 用 ifPresent 和 orElse 替换 isPresent

英文:

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

questions.stream()
    .peek(question -&gt; {
            Employee employee = question.isBoolean()
                .map(b -&gt; Employee.builder().withBooleanValue(Boolean.valueOf(question.value())).build())
                .orElse(Employee.builder().withStringValue(question.value()).build());
            employeeValue.set(employee);
        }
    )
    .map(question -&gt; Record.builder().withId(question.id()).withValue(employeeValue.get()).build())
    .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:

确定