包含数组的Java可选

包含数组的Java可选,java,arrays,optional,Java,Arrays,Optional,在我所做的所有搜索中,我都找不到这样的例子。我的错:( 我有一个包含数组的可选对象。我现在需要遍历数组并在其中定位特定元素 代码和示例类如下所示: public class Component { private String name; public Component(String ipName) { this.name = ipName; } public String getName() { return name; }

在我所做的所有搜索中,我都找不到这样的例子。我的错:(

我有一个包含数组的可选对象。我现在需要遍历数组并在其中定位特定元素

代码和示例类如下所示:

public class Component {
   private String name;

   public Component(String ipName) {
      this.name = ipName;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

public class Container {
   private Component[] componentArray;

   public Container(Component[] ipComponentArray) {
      this.componentArray = ipComponentArray;
   }

   public Component[] getComponentArray() {
      return componentArray;
   }

   public void setComponentArray(Component[] componentArray) {
      this.componentArray = componentArray;
   }
}

public class TestClass {
   public static void main(String[] args) {
      Container newContainer = getNewContainer();
      System.out.println(checkIfComponentIsPresent("Two", newContainer)); //prints true
      System.out.println(checkIfComponentIsPresent("Five", newContainer)); //prints false
   }

   private static Container getNewContainer() {
      return new Container(new Component[] {new Component("One"), new Component("Two"), new Component("Three")});
   }

   private static boolean checkIfComponentIsPresent(String ipName, Container newContainer) {
      boolean isPresent = false;

      Optional<Component[]> componentArrayOptional = Optional.ofNullable(newContainer).map(Container::getComponentArray);
      if(componentArrayOptional.isPresent()) {
          Component[] componentArray = componentArrayOptional.get();
          if(componentArray != null && componentArray.length > 0) {
              for(Component component : componentArray) {
                  if(ipName.equals(component.getName())) {
                     isPresent = true;
                     break;
                  }
              }
          }
      }

      return isPresent;
   }
}
private static boolean checkIfComponentIsPresentUsingStreams(String ipName, Container newContainer) {
    boolean isPresent = false;

    Optional<Component[]> componentArrayOptional = Optional.ofNullable(newContainer).map(Container::getComponentArray);
    if(componentArrayOptional.isPresent()) {
        Stream<Component> componentArrayStream =  Arrays.stream(componentArrayOptional.get());
        isPresent = componentArrayStream.filter(component -> ipName.equals(component.getName())).findFirst().isPresent();
    }

    return isPresent;
}
但是我不能使用流,实际上我的类很大,数组本身可以包含很多元素。使用流会降低性能


谢谢!

您可以在
map
方法中使用实际对象值进行操作:

boolean isPresent = Optional.ofNullable(newContainer)
  .map(Container::getComponentArray)
  .map(arr -> {
    for (Component component : arr) {
      if (Objects.equals(component.getName(), ipName)) {
        return true;
      }
    }
    return false;
  })
  .orElse(false);
事实上,我确信是什么让你认为
Stream
会显著降低你的应用程序的速度。因此,有另一种使用streams的解决方案:

boolean isPresent = Optional.ofNullable(newContainer)
  .map(Container::getComponentArray)
  .map(arr -> Stream.of(arr).anyMatch(component -> Objects.equals(ipName, component.getName())))
  .orElse(false);

使用流,会降低性能
,你确定吗?谢谢@soon!但是你能告诉我是什么让它变慢的吗,因为它确实会变慢。使用
anyMatch
过滤器
findFirst
更好吗?没有实际数据(或者至少数据大小)我不能说任何有价值的东西。
anyMatch
filter
+
findFirst
应该以类似的方式运行,因为
filter
是惰性的