0

In all the searching I did, I could not find an example of this sort. My bad :(

I have an Optional object containing an array. I now need to traverse the array and locate a particular element inside it.

Codes and sample classes as follows:

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;
   }
}

Can someone please advise me how can I improve the method checkIfComponentIsPresent? I want to know how can we traverse an array inside an Optional object, without converting it into a list or stream.

I can do it using streams as follows:

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;
}

But I cannot use streams, actually my classes are huge, and the array itself can contain numerous elements. Using streams, will degrade the performance.

Thanks!

1
  • 4
    Using streams, will degrade the performance, are you sure? Commented May 12, 2018 at 12:55

1 Answer 1

1

You can operate with actual object value inside map method:

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);

Actually I am sure what makes you think Stream would slow down your app significantly. And therefore there is another solution using streams:

boolean isPresent = Optional.ofNullable(newContainer)
  .map(Container::getComponentArray)
  .map(arr -> Stream.of(arr).anyMatch(component -> Objects.equals(ipName, component.getName())))
  .orElse(false);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @soon! But can you please advise me what is making it slower, as it really does make it. Is using anyMatch better over filter and findFirst
Without actual data (or at least data size) I cannot say anything valuable. Both anyMatch and filter + findFirst should behave in a similar way, since filter is lazy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.