Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

237
Vistas
How to handle NumberFormatException with Java StreamAPI

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()));
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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
over 4 years ago · Santiago Trujillo Denunciar

0

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

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda