如何使用Stream API从对象中检索嵌套列表?

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

How to retrieve nested list from object using Stream API?

问题

Sure, here is the translated code without the translation of the code itself:

@Data
public class TopComplexity {
   List<SuperComplexProperty> superComplexProperties;
}
@Data
public class SuperComplexProperty {
   List<SimpleProperty> simpleProperties;
   ComplexProperty complexProperty;
}

@Data
public class ComplexProperty {
   List<SimpleProperty> simpleProperties;
}

public class MainClass {
   public static void main(String[] args) {

       TopComplexity top = null;
       List<SimpleProperty> result = new ArrayList<>();

       for(SuperComplexProperty prop : top.getSuperComplexProperties) {
          result.addAll(prop.getSimpleProperties());

          if(Objects.nonNull(prop.getComplexProperty())) {
              result.addAll(prop.getComplexProperty().getSimpleProperties());
         }
      }
   }
}

I have removed the translation of the code as requested. If you have any further questions or need assistance with the code, please feel free to ask.

英文:

Do you have any idea how to retrieve all SimpleProperty from TopComplexity object?
I need to change that for loop into stream "kind" piece of code.

@Data
public class TopComplexity {
   List&lt;SuperComplexProperty&gt; superComplexProperties;
}
@Data
public class SuperComplexProperty {
   List&lt;SimpleProperty&gt; simpleProperties;
   ComplexProperty complexProperty;
}

@Data
public class ComplexProperty {
   List&lt;SimpleProperty&gt; simpleProperties;
}

public class MainClass {
   public static void main(String[] args) {

       TopComplexity top = null;
       List&lt;SimpleProperty&gt; result = new ArrayList&lt;&gt;();
      
       for(SuperComplexProperty prop : top.getSuperComplexProperties) {
          result.addAll(prop.getSimpleProperties());
      
          if(Objects.nonNull(prop.getComplexProperty()) {
              result.addAll(prop.getComplexProperty().getSimpleProperties());
         }
      }
   }
}

Really appreciate any kind of help

答案1

得分: 2

你可以将flatMap与涉及Stream的连接和三元运算符混合使用,例如:

List<SimpleProperty> result = top.getSuperComplexProperties().stream()
        .flatMap(scp -> Stream.concat(
                scp.getSimpleProperties().stream(),
                scp.getComplexProperty() == null ?
                        Stream.empty() :
                        scp.getComplexProperty().getSimpleProperties().stream()))
        .collect(Collectors.toList());
英文:

You can mix up flatMap with a concatenation and ternary operator involving Streams such as:

List&lt;SimpleProperty&gt; result = top.getSuperComplexProperties().stream()
        .flatMap(scp -&gt; Stream.concat(
                scp.getSimpleProperties().stream(),
                scp.getComplexProperty() == null ?
                        Stream.empty() :
                        scp.getComplexProperty().getSimpleProperties().stream()))
        .collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年7月31日 17:39:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63189341.html
匿名

发表评论

匿名网友

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

确定