Q:

Java 8 : Cannot invoke orElseThrow() on the primitive type void

I was trying the following code: 
Optional.ofNullable(listEvidence).ifPresent(
            eviList -> {
                List<String> repoKeysList = new ArrayList<String>();
                for (Evidence evidence : eviList) {
                    repoKeysList.add(evidence.getRepositoryKey());
                }   
                log.debug("UserContentBean: processRepoSyncCleanup repoKeysList - "+repoKeysList);
                evidenceDetailDAO.updateIsSelectedFlag(repoKeysList, 0,configurationService.getNodeName());
            }
            ).orElseThrow();

but I am getting the following error:
Cannot invoke orElseThrow() on the primitive type void

It seems to me that orElseThrow() is misplaced in the code. but updateIsSelectedFlag() throws an exception which needs to be handled.
Also, this piece of code is a part of, say, method1(). I tried adding throws Exception to method1(), but it yet asks me to handle the exception raised by updateIsSelectedFlag(). I couldn't understand the reason for the same.

A:

This seems like a poor approach to the situation.  In general, speaking on the use of Optional, you're expecting that usually as a return value, not a value that you do operations on.  In effect, you're still doing a null check; it's just unnecessarily obfuscated.
Instead, perform the operations you intend to do with the if statement.
if (null != listEvidence) {
    List<String> repoKeysList = new ArrayList<String>();
    for (Evidence evidence : listEvidence) {
        repoKeysList.add(evidence.getRepositoryKey());
    }
    log.debug("UserContentBean: processRepoSyncCleanup repoKeysList - " + repoKeysList);
    evidenceDetailDAO.updateIsSelectedFlag(repoKeysList, 0, configurationService.getNodeName());
}

For more context into the conversation around Optional, this Stack Overflow question goes into some detail as to what use cases Optional is meant for.

A:

Optional.ifPresent returns a void type, and not the Optional itself.
What you're trying to do should be done with a  if {listEvidence != null} else {} block. 

A:

You are misusing the Optional class.
First problem is that inside ifPresent() you are using a lambda expression that contains code that throws a checked exception. Lambdas cannot throw checked excpetions (i.e those that do not extend RuntimeException).
Second problem is that you are misusing the orElseThrow() method of Optional. It is supposed to be used to either get a maybe nullable object or throw an exception if it is actually null like so:
MyObject notNull = Optional.ofNullable(maybeNull).orElseThrow(new MyException());

For your use case you can either go with the old school if-else or if you want to play with Optional for some reason the way to go about it would be:
Optional.ofNullable(listEvidence).ifPresent((eviList -> {
  // your logic
  try {
    evidenceDetailDAO.updateIsSelectedFlag(repoKeysList, 0,configurationService.getNodeName());
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
});

This way you convert the checked exception into an unchecked one inside the lambda and you can handle it outside the Optional by adding another try-catch for RuntimeException.

