continue doesn't work when using streams and the map

dernor00

I have this simple code where I use a stream and a .map() function. I do a null check for the id, and inside it a add a continue The continue gives me an error: Continue outside of loop When I remove the continue I don't get an error, but I don't know if the behaviour is the same?

public List<Long> getIds(final Long[][] value){
     List<Long> list = Arrays.stream(value).map(result ->{
                final Long id = result[1];
                if(id == null){
                    continue; // This part doesn't work (error: Continue outside of loop)
                }
                return id;
            }).collect(Collectors.toList());
}

Any suggestion on why this happens with .streams? Whereas, when I don't use the stream I can use continue.

The question has been marked as duplicate, but it's not the case. Using return surely works in forEach, where no return type is requested, but not in map.

Andronicus

continue works in a for loop. You can use flatMap as a workaround:

 List<Long> list = Arrays.stream(value).flatMap(result ->{
            final Long id = result[1];
            return Stream.ofNullable(id);
        }).collect(Collectors.toList());

You can also make it more concise by using Stream.ofNullable directly as @Naman suggests:

 List<Long> list = Arrays.stream(value)
    .flatMap(result -> Stream.ofNullable(result[1]))
    .collect(Collectors.toList());

The other, more elegant version of my firstly proposed method by @Holger would be to use the predicate in the filter:

 List<Long> list = Arrays.stream(value)
    .map(result -> result[1])
    .filter(Objects::nonNull)
    .collect(Collectors.toList());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

continue doesn't work when using streams and the map

From Dev

Continue For doesn't work

From Dev

Implementing iterator interface with map doesn't work with java 8 streams

From Dev

Java program using ProcessBuilder with input and output streams doesn't work

From Dev

Java program using ProcessBuilder with input and output streams doesn't work

From Dev

Labelled "continue" doesn't seem to work

From Dev

Labelled "continue" doesn't seem to work

From Dev

Continue statement in plpgsql doesn't work

From Dev

mergeMaps doesn't work when first map has no elements?

From Java

Ignore duplicates when producing map using streams

From Dev

Google Map doesn't fully expand when using Toggle

From Dev

Google map doesn't display when using ui-router

From Dev

Redirect a user after registration when using Devise doesn't work?

From Dev

Code doesn't work as expected when using parameters

From Dev

Using WHERE IN (...) with PDO doesn't work when the string is bound

From Dev

PHP script doesn't work when using wamp server

From Dev

Using NuGet with WIX doesn't work when building release

From Dev

Overflow doesn't work when using display:table-cell

From Dev

Success function in ajax when using setInterval doesn't work

From Dev

Flexbox sizing doesn't work correctly when using FontAwesome

From Dev

The success Function in Ajax Doesn't work When using Json

From Dev

menuItem.setVisible doesn't work when using a Fragment

From Dev

Using WHERE IN (...) with PDO doesn't work when the string is bound

From Dev

PHP $_GET values doesn't work when using rewrite rules

From Dev

jQuery .data doesn't work when using a html jQuery object

From Dev

Why doesn't or operator work in if statement when using "not equals"?

From Dev

Script doesn't work when it is run using '#!/bin/sh'

From Dev

curl_exec to Mailchimp doesn't work when using AJAX

From Dev

Depth testing doesn't work when using custom framebuffer

Related Related

  1. 1

    continue doesn't work when using streams and the map

  2. 2

    Continue For doesn't work

  3. 3

    Implementing iterator interface with map doesn't work with java 8 streams

  4. 4

    Java program using ProcessBuilder with input and output streams doesn't work

  5. 5

    Java program using ProcessBuilder with input and output streams doesn't work

  6. 6

    Labelled "continue" doesn't seem to work

  7. 7

    Labelled "continue" doesn't seem to work

  8. 8

    Continue statement in plpgsql doesn't work

  9. 9

    mergeMaps doesn't work when first map has no elements?

  10. 10

    Ignore duplicates when producing map using streams

  11. 11

    Google Map doesn't fully expand when using Toggle

  12. 12

    Google map doesn't display when using ui-router

  13. 13

    Redirect a user after registration when using Devise doesn't work?

  14. 14

    Code doesn't work as expected when using parameters

  15. 15

    Using WHERE IN (...) with PDO doesn't work when the string is bound

  16. 16

    PHP script doesn't work when using wamp server

  17. 17

    Using NuGet with WIX doesn't work when building release

  18. 18

    Overflow doesn't work when using display:table-cell

  19. 19

    Success function in ajax when using setInterval doesn't work

  20. 20

    Flexbox sizing doesn't work correctly when using FontAwesome

  21. 21

    The success Function in Ajax Doesn't work When using Json

  22. 22

    menuItem.setVisible doesn't work when using a Fragment

  23. 23

    Using WHERE IN (...) with PDO doesn't work when the string is bound

  24. 24

    PHP $_GET values doesn't work when using rewrite rules

  25. 25

    jQuery .data doesn't work when using a html jQuery object

  26. 26

    Why doesn't or operator work in if statement when using "not equals"?

  27. 27

    Script doesn't work when it is run using '#!/bin/sh'

  28. 28

    curl_exec to Mailchimp doesn't work when using AJAX

  29. 29

    Depth testing doesn't work when using custom framebuffer

HotTag

Archive