0

I am trying to convert a list of patterns (whose type is string) into predicates and for some reasons my brain keeps ending up with something close to the following code:

    List<Predicate> predicates = patterns.stream()
        .map(Pattern::asPredicate)
        .collect(Collectors.toList());

which obviously doesn't compile. What's the best way of writing the conversion stream?

This is the error message I am getting:

Provider.java:32: error: method map in interface Stream<T> cannot be applied to given types;
            .map(Pattern::asPredicate)
            ^
  required: Function<? super String,? extends R>
  found: Pattern::a[...]icate
  reason: cannot infer type-variable(s) R
    (argument mismatch; invalid method reference
      method asPredicate in class Pattern cannot be applied to given types
        required: no arguments
        found: String
        reason: actual and formal argument lists differ in length)
  where R,T are type-variables:
    R extends Object declared in method <R>map(Function<? super T,? extends R>)
    T extends Object declared in interface Stream
MsisdnPartitioningProvider.java:32: error: invalid method reference
            .map(Pattern::asPredicate)
                 ^
  non-static method asPredicate() cannot be referenced from a static context
Note: MsisdnPartitioningProvider.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors
Sandah Aung
  • 6,156
  • 15
  • 56
  • 98

1 Answers1

1

You have mentioned that patterns are String, i.e. it might be List<String>, so you might want to try below:

List<Predicate> predicates = patterns.stream().map(Pattern::compile).map(Pattern::asPredicate)
                .collect(Collectors.toList());

Note: you can use List<Predicate<String>> to avoid raw type warning as user @MC Emperor mentioned

Ashish Patil
  • 4,428
  • 1
  • 15
  • 36
  • The compiler is complaining about a different problem now `Provider.java:36: error: invalid method reference .map(Pattern::asMatchPredicate)` – Sandah Aung May 12 '22 at 10:04
  • Above suggestion is provided based on what you have said, `patterns` are `Strings` meaning, something like this; `List patterns = new ArrayList<>();` If your `patterns` are different please let us know exact type. – Ashish Patil May 12 '22 at 10:08
  • Patterns are indeed strings. `List patterns = Arrays.asList(rawPatterns);` – Sandah Aung May 12 '22 at 10:14
  • @SandahAung - updated Answer, please let us know if it works, – Ashish Patil May 12 '22 at 10:20
  • The problem I think is a little more subtle: `non-static method asPredicate() cannot be referenced from a static context` – Sandah Aung May 12 '22 at 10:47
  • @SandahAung , I am not getting `non-static method asPredicate() cannot be referenced from a static context` error when I tried. – Ashish Patil May 12 '22 at 10:51
  • I think what I want to achieve is more similar to [this example](https://stackoverflow.com/questions/24882927/using-streams-to-convert-a-list-of-objects-into-a-string-obtained-from-the-tostr) – Sandah Aung May 13 '22 at 02:04