Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - how do I check if a string exists in each of the elements of a HashSet

    final HashSet<String> VALUES = new HashSet<String>(Arrays.asList(new String[] {"OK (0.07, 0.05, 0.01)",
            "OK (0.07, 0.05, 0.02)",
            "OK (0.07, 0.05, 0.03)",
            "OK (0.07, 0.05, 0.04)"}));
    String s="OK";
    System.out.println(VALUES.contains(s));

Gives me false. How do I check if "OK" exists in each of the elements?

like image 555
rhanabe Avatar asked Jan 22 '26 03:01

rhanabe


1 Answers

Currently, you're checking if your Set contains the value OK. If you want to check if each of the elements of the Set contains OK (note the difference), you'll need to loop through the values of your Set like the others stated.

Using java-8 and streams, you can also do :

boolean b = VALUES.stream().allMatch(s -> s.contains("OK"));
like image 190
Alexis C. Avatar answered Jan 23 '26 20:01

Alexis C.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!