Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Predicate<? super String> s) or (String s)

I have a TreeSet of Strings (hardcoded). Want to check that a given parameter String eg. "Person" if present in the TreeSet then return true otherwise return false.

Here I am confused by the Eclipse message regarding (Predicate<? super String> s) vs (String s):

The method anyMatch(Predicate) in the type Stream is not applicable for the arguments (String)

Please guide.

import java.util.Set;
import java.util.TreeSet;
import java.util.function.Predicate;

public class SystemLabelValidator {

    public static boolean ValidateSystemLabel( String s) {  

        String j = s;

        boolean b = false;

        Set <String> SystemLabels = new TreeSet<String>();
        // Unique Strings
        SystemLabels.add("Person");
        SystemLabels.add("Player");
        SystemLabels.add("Hospital");
        SystemLabels.add("Nurse");
        SystemLabels.add("Room");

        System.out.println("\n==> Loop here.");
        for (String temp : SystemLabels) {
            System.out.println(temp);

            if(SystemLabels.stream().anyMatch(j)) {
                System.out.println("I need to return Boolean");
            }
            return b;
        }
        return b;
    }
}
like image 488
Optimight Avatar asked Feb 22 '19 18:02

Optimight


Video Answer


3 Answers

There is no need to use a Predicate here. In order to check if the String is present in your TreeSet just use :

return systemLabels.contains("Person");

If you still insist on using anyMatch then you can do :

public static boolean validateSystemLabel(String s) {
    return systemLabels.stream().anyMatch(i -> i.equals(s));
}

Remember, a predicate expression needs to evaluate to a boolean value but in the code, you are passing in a String hence the compilation error.

like image 134
Nicholas Kurian Avatar answered Sep 16 '22 15:09

Nicholas Kurian


The problem in your solution is this line:

SystemLabels.stream().anyMatch(j);

Basically anyMatch() expects Predicate as input not String.

But your problem has simpler solution:

import java.util.Set;
import java.util.TreeSet;

public class SystemLabelValidator {

    private static final Set<String> SYSTEM_LABLES = new TreeSet<>(Arrays.asList("Person", "Player", "Hospital", "Nurse", "Room"));

    public static boolean validateSystemLabel( String value) {  
        return SYSTEM_LABLES.contains(value);
    }

}
like image 20
Oleksii Zghurskyi Avatar answered Sep 18 '22 15:09

Oleksii Zghurskyi


The signature of anyMatch is

boolean anyMatch(Predicate<? super T> predicate)

In your case, the argument must be a Predicate<? super String>. That is, a method which can take a string and return a boolean. This method is looking for one of the following

Predicate<String> (e.g. String::isEmpty)
Predicate<Object> (e.g. Objects::isNull)
Predicate<CharSequence>
Predicate<Comparable<String>>
Predicate<Serializable>

You have attempted to give it a string, which does not match the signature. One way to fix this would be:

if(SystemLabels.stream().anyMatch(j::equals)) {
    System.out.println("I need to return Boolean");
}
like image 21
Michael Avatar answered Sep 16 '22 15:09

Michael