allMatch in Java ?
- allMatch() is used to verify each value in list should match with provided input/predicate.
- Returns boolean value (true/false)
- We can also use string in built functions (like toLowerCase(), StartWith,endsWith() etc.,.) to verify each value in given list.
allMatch Java8 program:
import java.util.*;
public class allMatch {
public static void main(String args[]) {
List<String> keywords = Arrays.asList("email", "Email", "EMAIL desk", "ABCDE");
boolean result1 = keywords.stream().allMatch(r -> r.toLowerCase().contains("email"));
System.out.println("result1=" + result1);
List<String> terms = Arrays.asList("email", "Email", "EMAIL desk", "eMaIl");
boolean result2 = terms.stream().allMatch(r -> r.toLowerCase().contains("email"));
System.out.println("result2=" + result2);
List<String> words = Arrays.asList("email", "Email");
boolean result3 = words.stream().allMatch(r -> r.contains("email"));
System.out.println("result3=" + result3);
List<String> keys = Arrays.asList("email", "email");
boolean result4 = keys.stream().allMatch(r -> r.contains("email"));
System.out.println("result4=" + result4);
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
boolean result5 = numbers.stream().allMatch(r -> (r > 0 && r < 7));
System.out.println("result5=" + result5);
List<Integer> number = Arrays.asList(1, 2, 3, 10);
boolean result6 = number.stream().allMatch(r -> (r > 20));
System.out.println("result6=" + result6);
List<String> fun = Arrays.asList("email", "email one");
boolean result7 = fun.stream().allMatch(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().allMatch(r -> r.startsWith("ema") && r.endsWith("age"));
System.out.println("result8=" + result8);
}
}
Output:
No comments:
Post a Comment
Please comment below to feedback or ask questions.