标签:sys date day nbsp div which 逻辑操作 支持 ack
package com.mytest; import java.util.Arrays; import java.util.List; import java.util.function.Predicate; public class LambdaTest { public static void main(String[] args) { List<String> strings = Arrays.asList("java","javaqwer","jsdfava", "merry", "tomorrow", "day"); System.out.println("which start with j:"); filter2(strings, (lan) -> lan.startsWith("j")); filter(strings, (str) -> str.startsWith("J")); System.out.println("Languages which ends with a "); filter(strings, (str) -> str.endsWith("a")); System.out.println("Languages which length>5"); filter(strings, (str) -> str.length() > 5); System.out.println("Languages which contains a"); filter(strings, (str) -> str.contains("a")); System.out.println("==========逻辑函数合并使用=========="); //逻辑函数合并使用 strings.stream().filter(startsWithJ.and(lengthOverFour)).forEach(str-> System.out.println(str)); }
//常规方式 private static void filter(List<String> strings, Predicate<String> condition) { for (String string : strings) { if (condition.test(string)) { System.out.println(string); } } }
//filter方法的改进版本 private static void filter2(List<String> strings, Predicate<String> condition) { strings.stream().filter(str -> (condition.test(str))).forEach(str -> System.out.println(str)); } //Predicate允许多个合成一个,它提供了逻辑操作 and,or等方法,把这些拼接起来传入filter()方法中。如主函数方法中的最后一行代码 static Predicate<String> startsWithJ = (n) -> n.startsWith("j"); static Predicate<String> lengthOverFour = (n) -> n.length() > 4; }
标签:sys date day nbsp div which 逻辑操作 支持 ack
原文地址:https://www.cnblogs.com/dayu007/p/12228676.html