Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter(Objects::nonNull) seems not work #5237

Closed
blackdiz opened this issue Aug 3, 2022 · 4 comments
Closed

filter(Objects::nonNull) seems not work #5237

blackdiz opened this issue Aug 3, 2022 · 4 comments
Assignees

Comments

@blackdiz
Copy link

blackdiz commented Aug 3, 2022

Hi, when I try filter(Objects::nonNull) to filter out any null value and then use map to apply a lambda, the checker framework seems to complain about the incompatible receiver type, here's a simple test example:

        @Test
	void test() {
		List<@Nullable String> s = new ArrayList<>();
		s.add("abc");
		s.add(null);
		s.add("cdf");
		System.out.println(s.stream().filter(Objects::nonNull).map(String::length).toList());
	}

And I got the following error:

error: [methodref.receiver] Incompatible receiver type
		System.out.println(s.stream().filter(Objects::nonNull).map(String::length).toList());
		                                                           ^
  found   : @Initialized @NonNull String
  required: @Initialized @Nullable String
  Consequence: method in @Initialized @NonNull String
    @Initialized @NonNull int length(@Initialized @NonNull String this)
  is not a valid method reference for method in @Initialized @NonNull Function<@Initialized @Nullable String, @Initialized @NonNull Integer>
    @Initialized @NonNull Integer apply(@Initialized @NonNull Function<@Initialized @Nullable String, @Initialized @NonNull Integer> this, @Initialized @Nullable String p0)

Is this a correct behavior? How can I make the code work?

@mernst
Copy link
Member

mernst commented Aug 5, 2022

Thank you for the bug report. This is possibly related to #979. @smillst will look into it, probably in early September.

@blackdiz
Copy link
Author

blackdiz commented Aug 6, 2022

Thank you, I find if I use collect(toList()) after filter, everything works fine:

     @Test
      void test() {
          List<@Nullable String> s = new ArrayList<>();
          s.add("abc");
          s.add(null);
          s.add("cdf");
          System.out.println(
              s.stream().filter(Objects::nonNull).collect(Collectors.toList()).stream()
                  .map(String::length)
                  .toList());
    }

@blackdiz blackdiz closed this as completed Aug 6, 2022
@blackdiz blackdiz reopened this Aug 6, 2022
@mernst
Copy link
Member

mernst commented Aug 6, 2022

Thanks for the additional information; that is useful.

@smillst
Copy link
Member

smillst commented May 21, 2024

This is a duplicate of #1345.

@smillst smillst closed this as completed May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants