从任一对象字段设置字符串值

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

Setting String value from either one object field

问题

String Name = null;

Optional<String> optA = Optional.ofNullable(obj1.getX())
    .map(a -> a.getXX())
    .map(b -> b.getName());

if (optA.isPresent()) {
    name = optA.get();
} else {
    Optional<String> optB = Optional.ofNullable(obj1.getY())
        .map(a -> a.getYY())
        .map(b -> b.getYYY())
        .map(c -> c.getName());
        
    if (optB.isPresent()) {
        name = optB.get();
    }
}
英文:

How can I make my code look better?

I want to set String Name to ob1j.getX().getXX().getName() if it is present, otherwise I would set name to obj1.getY().getYY().getYYY().getName(). I'm also using Optional to perform null check.

Is this the correct way to do it ?

I'm using JDK8 so 'ifPresentOrElse' is not an option.

String Name = null;

Optional&lt;String&gt; optA = Optional.ofNullable(obj1.getX())
        .map(a -&gt; a.getXX())
        .map(b -&gt; b.getName())

if (optA.isPresent()) {
    name = optA.get();
} else {
    Optional&lt;String&gt; optB = Optional.ofNullable(obj1.getY())
		.map(a -&gt; a.getYY())
		.map(b -&gt; b.getYYY())
		.map(c -&gt; c.getName())
    if (optB.isPresent()) {
        name = optB.get();
    }
}

答案1

得分: 0

你可以这样做:

name = Stream.of(
         Optional.ofNullable(obj1.getX()).map(X::getXX).map(XX::getName),
         Optional.ofNullable(obj1.getY()).map(Y::getYY).map(YY::getYYY).map(YYY::getName)
    )
     .filter(Optional::isPresent)
     .map(Optional::get)
     .findFirst().orElse(null);

或者对于延迟处理,你可以像这样使用 Supplier

List<Supplier<Optional<String>>> suppliers = Arrays.asList(
            () -> Optional.ofNullable(obj1.getX()).map(X::getXX).map(XX::getName),
  () -> Optional.ofNullable(obj1.getY()).map(Y::getYY).map(YY::getYYY).map(YYY::getName));

name = suppliers.stream()
            .map(Supplier::get)
            .filter(Optional::isPresent)
            .map(Optional::get)
            .findFirst().orElse(null);
英文:

You can do like this:

name = Stream.of(
         Optional.ofNullable(obj1.getX()).map(X::getXX).map(XX::getName),
         Optional.ofNullable(obj1.getY()).map(Y::getYY).map(YY::getYYY).map(YYY::getName)
    )
     .filter(Optional::isPresent)
     .map(Optional::get)
     .findFirst().orElse(null);

Or for lazy processing you use Supplier like this:

List&lt;Supplier&lt;Optional&lt;String&gt;&gt;&gt; suppliers =Arrays.asList(
            () -&gt; Optional.ofNullable(obj1.getX()).map(X::getXX).map(XX::getName),
  () -&gt; Optional.ofNullable(obj1.getY()).map(Y::getYY).map(YY::getYYY).map(YYY::getName));

name = suppliers.stream()
            .map(Supplier::get)
            .filter(Optional::isPresent)
            .map(Optional::get)
            .findFirst().orElse(null);

huangapple
  • 本文由 发表于 2020年4月8日 12:09:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/61093188.html
匿名

发表评论

匿名网友

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

确定