Home > Back-end >  How to handle NumberFormatException with Java StreamAPI
How to handle NumberFormatException with Java StreamAPI

Time:04-11

Is there a way to filter out all values that are bigger than the max value that can be stored in a Long using Stream API?

The current situation is that you can search in the frontend with a simple search bar after some customers by using their ID.

For example: 123456789, 10987654321. If you put a "separator" between these two IDs, everything works. But if you forget the "separator" my code is trying to parse 12345678910987654321 into a Long and I guess there is the problem.

That causes a NumberFormatException after trying to search. Is there a way to filter these numbers out that can't be parsed into a Long because they are too big?

String hyphen = "-";

String[] customerIds = bulkCustomerIdProperty.getValue()
              .replaceAll("[^0-9]", hyphen)
              .split(hyphen);
...
customerFilter.setCustomerIds(Arrays.asList(customerIds).stream()
              .filter(n -> !n.isEmpty()) 
              .map(n -> Long.valueOf(n)) // convert to Long
              .collect(Collectors.toSet()));

CodePudding user response:

You can either extract parsing into a separate method and wrap it with a try/catch, or use BigInteger to eliminate values that exceed the range of long.

Example with BigInteger:

Set<Long> result =  Stream.of("", "12345", "9999999999999999999999999999")
        .filter(n -> !n.isEmpty())
        .map(BigInteger::new)
        .filter(n -> n.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) <= 0 &&
                     n.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) >= 0)
        .map(BigInteger::longValueExact) // convert to Long
        .peek(System.out::println) // printing the output
        .collect(Collectors.toSet());

Example with handling NumberFormatException in a separate method:

Set<Long> result =  Stream.of("", "12345", "9999999999999999999999999999")
        .filter(n -> !n.isEmpty())
        .map(n -> safeParse(n))
        .filter(OptionalLong::isPresent)
        .map(OptionalLong::getAsLong) // extracting long primitive and boxing it into Long
        .peek(System.out::println) // printing the output
        .collect(Collectors.toSet());

public static OptionalLong safeParse(String candidate) {
    try {
        return OptionalLong.of(Long.parseLong(candidate));
    } catch (NumberFormatException e) {
        return OptionalLong.empty();
    }
}

Output (from peek())

12345

CodePudding user response:

Maybe you could add another filter like

.filter((n) -> { return new BigInteger(n).compareTo(new BigInteger(String.valueOf(Long.MAX_VALUE))) <= 0;})

You could also use a try/catch to throw an error when this happens and notify your frontend

  • Related