Pages

anyMatch in Java

 anyMatch in Java ?

  • anyMatch() is used to verify each value in list is matched or not with provided input/predicate.
  • Returns true if any elements of the stream match the provided predicate, otherwise false.
  • We can also use string in built functions (like toLowerCase(), StartWith,endsWith() etc.,.) to verify each value in given list. 
anyMatch Java8 program:
import java.util.*;

public class anyMatch {
public static void main(String args[]) {
List<String> keywords = Arrays.asList("email", "Email", "EMAIL desk", "ABCDE");
boolean result1 = keywords.stream().anyMatch(r -> r.toLowerCase().contains("email"));
System.out.println("result1=" + result1);

List<String> terms = Arrays.asList("email", "Email", "EMAIL desk", "eMaIl");
boolean result2 = terms.stream().anyMatch(r -> r.toLowerCase().contains("email"));
System.out.println("result2=" + result2);

List<String> words = Arrays.asList("linkedin", "twitter");
boolean result3 = words.stream().anyMatch(r -> r.contains("email"));
System.out.println("result3=" + result3);

List<String> keys = Arrays.asList("email", "email");
boolean result4 = keys.stream().anyMatch(r -> r.contains("email"));
System.out.println("result4=" + result4);

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
boolean result5 = numbers.stream().anyMatch(r -> (r > 0 && r < 7));
System.out.println("result5=" + result5);

List<Integer> number = Arrays.asList(1, 2, 3, 10);
boolean result6 = number.stream().anyMatch(r -> (r > 20));
System.out.println("result6=" + result6);

List<String> fun = Arrays.asList("email", "email one");
boolean result7 = fun.stream().anyMatch(r -> r.startsWith("ema") && r.endsWith("il"));
System.out.println("result7=" + result7);

List<String> funs = Arrays.asList("email message", "email one message");
boolean result8 = funs.stream().anyMatch(r -> r.startsWith("ema") && r.endsWith("age"));
System.out.println("result8=" + result8);
}
}
Output:
anyMatch in java
anyMatch() in Java

Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.