5

I have a string:

"1, 2, 3 , -4"

it is split by ", ". I have a function to convert a number to a enum type which works fine. I want to use java 8 to convert this string to a list of enum objects.

Pattern pattern = Pattern.compile(", ");
List<f> fList = pattern.splitAsStream(str)
  .map(s -> {
    try {
      return this.getEnumObject(Integer.valueOf(s), f.class);
    }
    catch (NoEleException e) {
      e.printStackTrace();
    }
  })
  .collect(Collectors.toList());

This gives me an error:

missing return type.

How could I fix it?

4
  • If you're suppressing the exception, you need to return some value. What value is up to you. Commented Nov 15, 2017 at 23:39
  • So what would you like to go into your list in the event that some of the numbers don't resolve to an enum object? Commented Nov 15, 2017 at 23:57
  • @misha it will never happen..but if it happens, just a null object is fine. Commented Nov 16, 2017 at 0:01
  • 1
    if you are convinced it will never happen, you can throw new RuntimeException(e); in the catch block. Commented Nov 16, 2017 at 0:08

4 Answers 4

6

Currently, if an exception occurs no result will be returned hence the compilation error. You'll need to return a value after the catch block .

Sign up to request clarification or add additional context in comments.

1 Comment

that will not give the result he is looking for, what would that value be?
5

Basically to ways of managing this:

  1. catching the exception and return some value or encapsulate values in Optionals and filter accordingly
  2. Throwing a RuntimeException which chains the original one

In the first case we use Optional to put something into the stream on error, and then manage these empty values further in the stream:

pattern.splitAsStream(str)
.map(s -> {
  try {
    return Optional.of(this.getEnumObject(Integer.valueOf(s), f.class));
  }
  catch (NoEleException e) {
    e.printStackTrace();
    return Optional.empty();
  }
 })
.filter(Optional::isPresent) // remove empty optionals
.map(Optional::get) // unwrap them
.collect(Collectors.toList());

In the second case the stream is stopped and you can then try to catch the RuntimeException and unchain the original one:

pattern.splitAsStream(str)
.map(s -> {
  try {
    return Optional.of(this.getEnumObject(Integer.valueOf(s), f.class));
  }
  catch (NoEleException e) {
    e.printStackTrace();
    throw new RuntimeException(e); // stop the stream
  }
 })
.collect(Collectors.toList());

Comments

0

You can create Null Object like MissingElement, return it in catch and them filter it out after map.

Comments

0

If you are certain that this won't happen you could return null in the catch and filter for non null before collecting:

Pattern pattern = Pattern.compile(", ");
List<f> fList = pattern.splitAsStream(str)
  .map(s -> {
    try {
      return this.getEnumObject(Integer.valueOf(s), f.class);
    }
    catch (Exception e) {
      return null;
    }
  })
  .filter(Objects::nonNull)
  .collect(Collectors.toList());

Comments

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.