java延时执行_Java谓词的延迟执行
java延時執行
在先前的文章“ 用Java的供應商延遲執行 ”和“ Java的消費者延遲執行 “,我看著很容易地通過推遲標準Java API接受,分別在Java執行供應商 S和消費者秒。 在本文中,我將對標準JDK提供的API如何通過標準功能接口Predicate允許延遲執行進行類似的研究。 Predicate 在其Javadoc中進行了描述 ,“代表一個參數的謂詞(布爾值函數)。” 換句話說, Predicate就像JDK提供的函數一樣 ,但是其返回值限制為true或false 。
標準Java API中Predicate的最常見應用可能是在過濾器的上下文中。 這篇Predicate中的幾個示例將演示Predicate結合過濾方法對Optional實例和Stream實例的使用。
Optional.filter(謂詞)
可選類的filter(Predicate)方法的行為通過其Javadoc文檔進行了描述,“如果存在值,并且該值與給定謂詞匹配,則返回描述該值的Optional ,否則返回一個空的Optional 。 ” 換句話說, Optional.filter(Predicate)返回一個Optional ,如果原始Optional為空,或者應用于原始和當前Optional的Predicate解析為false ,則該Optional將為空。 否則,如果原始的Optional確實具有“ present”值,并且應用于該值的Predicate返回true ,則返回的Optional也將具有相同的“ present”值。 下一個代碼清單對此進行了說明(完整的源代碼在GitHub上可用 )。
展示了Optional.filter(Predicate)
/*** Demonstrate use of {@code Optional.filter(Predicate)} on an* {@code Optional<Boolean>}.*/ public static void demonstrateOptionalFilterOnBoolean() {out.print("\nfalse: ");getOptionalBoolean(false).filter(b -> b).ifPresent(out::print);out.print("\ntrue: ");getOptionalBoolean(true).filter(b -> b).ifPresent(out::print);out.print("\nnull: ");getOptionalBoolean(null).filter(b -> b).ifPresent(out::print); }/*** Demonstrate use of {@code Optional.filter(Predicate)} on an* {@code Optional<Float>}.*/ public static void demonstrateOptionalFilterOnFloat() {out.print("\n3.14: ");getOptionalFloat(3.14f).filter(f -> f > 0.0).ifPresent(out::print);out.print("\n-2.5: ");getOptionalFloat(-2.5f).filter(f -> f > 0.0).ifPresent(out::print);out.print("\nnull: ");getOptionalFloat(null).filter(f -> f > 0.0).ifPresent(out::print); }上面的代碼清單中的兩種方法演示了Optional.filter(Predicate)在基于數值比較的lambda表達式上產生直接boolean結果以及在lambda表達式上產生boolean結果的用法。 在一種情況下, Predicate是boolean ,在另一種情況下, Predicate是數值比較。
Stream.filter(謂詞)
Stream接口的方法filter(Predicate) 與同名的Optional類的方法相似 。 下面的代碼清單演示了Stream.filter(Predicate) 。
展示了Stream.filter(Predicate)
/*** Demonstrates use of {@code Stream.filter(Predicate}}.*/ public static void demonstrateStreamFilter() {final int maximum = 100;out.println("\nThe probable prime numbers between 1 and " + maximum + " are:");final Stream<BigInteger> bigIntegers = getConsecutiveBigIntegers(maximum);bigIntegers.filter(bi -> bi.isProbablePrime(100)).forEach(pp -> out.println(" " + pp)); }上面的代碼列表不是用于展示在Java識別素數的最佳方法。 相反,它意在表明如何filter(Predicate)可以在調用Stream來縮小該要素Stream ,只有那些符合Predicate 。
對于我的下圖Stream.filter(Predicate) ,我使用Pattern類的方便的方法asPredicate()供給的實例Predicate要被提供給使用這兩個例子Stream.filter(Predicate) 。
演示了Pattern.asPredicate()的Stream.filter(Predicate)
/*** Demonstrates use of {@code Pattern.asPredicate()} to provide* a {@code Predicate} that can be used with {@code Stream.filter()}.*/ public static void demonstratePatternAsPredicateInFilter() {final long count= getPotentialTelephoneNumbers().stream().filter(PATTERN.asPredicate()).peek(out::println).count();out.println(count + " valid telephone numbers."); }Collection.removeIf(謂詞)
Collection接口指定(并作為默認方法實現 )有用的方法removeIf(Predicate) 。 還有Collection多個實現,它們實現了它們自己的removeIf(Predicate)的覆蓋版本,包括ArrayDeque.removeIf(Predicate) , ArrayList.removeIf(Predicate)和Vector.removeIf(Predicate) 。
下一個代碼清單演示了運行中的Collection.removeIf(Predicate)兩個示例。 第一個示例使用Predicate.negate()方法取反期望的正則表達式模式,以便從集合中刪除的元素是與正則表達式不匹配的元素。 第二個示例執行類似的功能,但是利用了JDK 11引入的“非”方法來執行此否定操作。
展示了帶有否定的Pattern.asPredicate()的Collection.removeIf(Predicate)
/*** Demonstrates use of {@code Collection.removeIf(Predicate)}* in conjunction with {@code Predicate.negate()}.*/ public static void demonstrateCollectionRemoveIf() {final Set<String> telephoneNumbers = new HashSet<>(getPotentialTelephoneNumbers());telephoneNumbers.removeIf(PATTERN.asPredicate().negate());out.println(telephoneNumbers); }/*** Demonstrates use of {@code Collection.removeIf(Predicate)}* in conjunction with JDK 11-introduced {@code Predicate.not()}.*/ public static void demonstrateCollectionRemoveIfWithJdk11Not() {final Set<String> telephoneNumbers = new HashSet<>(getPotentialTelephoneNumbers());telephoneNumbers.removeIf(not(PATTERN.asPredicate()));out.println(telephoneNumbers); }Stream.allMatch(謂詞)
如果流中的每個元素都與提供的Predicate匹配,則Stream接口的方法allMatch(Predicate)返回true 。 如果甚至單個元素都不匹配Predicate ,則該方法返回false 。
展示了Stream.allMatch(Predicate)
/*** Demonstrate use of {@code Stream.allMatch(Predicate)}.*/ public static void demonstrateStreamAllMatch() {final Set<String> names = getNames();final boolean allNamesSixDigits = names.stream().allMatch(name -> name.length() == 6);out.println("Are all names " + names + " six digits? " + allNamesSixDigits); }Stream.anyMatch(謂詞)
所述Stream.anyMatch(謂詞)方法返回true ,如果它的元素中的至少一個相匹配的Predicate并返回false ,如果沒有它的元素的匹配Predicate 。
展示了Stream.anyMatch(Predicate)
/*** Demonstrate use of {@code Stream.anyMatch(Predicate)}.*/ public static void demonstrateStreamAnyMatch() {final Set<String> names = getNames();final boolean anyNamesSixDigits = names.stream().anyMatch(name -> name.length() == 6);out.println("Are any names " + names + " six digits? " + anyNamesSixDigits); }Stream.noneMatch(謂詞)
所述Stream.noneMatch(謂詞)方法返回true時在流沒有元素匹配Predicate并返回false如果流中的至少一種元素確實匹配的Predicate 。
展示了Stream.noneMatch(Predicate)
/*** Demonstrate use of {@code Stream.noneMatch(Predicate)}.*/ public static void demonstrateStreamNoneMatch() {final Set<String> names = getNames();final boolean noNamesSixDigits = names.stream().noneMatch(name -> name.length() == 6);out.println("Are no names " + names + " six digits? " + noNamesSixDigits);final boolean noNamesFourDigits = names.stream().noneMatch(name -> name.length() == 4);out.println("Are no names " + names + " four digits? " + noNamesFourDigits); }Collectors.partitioningBy(謂詞)
盡管還有更多使用 Predicate JDK API ,但本文將通過討論和使用Collectors.partitioningBy(Predicate)的示例進行總結。 這種有趣的方法將調用流的所有元素分為兩組,一組與鍵Boolean.TRUE相關聯(與Predicate匹配的元素),與鍵Boolean.FALSE相關聯的一組(不與鍵Boolean.FALSE關聯的組)匹配Predicate )。 下一個代碼清單利用這一點將整數分為偶數和奇數。
展示了Collectors.partitioningBy(Predicate)
/*** Demonstrate use of {@code Collectors.partitioningBy(Predicate)}.*/ public static void demonstrateCollectorsPartitioningBy() {final Map<Boolean, List<Integer>> evensAndOdds= getConsecutiveIntegers(100).collect(Collectors.partitioningBy(integer -> integer % 2 == 0));out.println("Evens: " + evensAndOdds.get(Boolean.TRUE));out.println("Odds: " + evensAndOdds.get(Boolean.FALSE)); }我在上面的代碼示例中使用了幾種“幫助程序”方法,這些在本文中未顯示。 這些“幫助程序”方法以及本文中顯示的所有示例都可以在GitHub上找到 。
Java的標準功能接口Predicate是內置Java功能接口Function的一個特殊版本,可以說應該得到自己的專業化,因為true / false返回狀態對于表示某些功能適用或不適用的條件非常有用。 這篇文章演示了JDK中的幾個實例,其中Predicate用于確定哪些流元素適用,是否使用Optional ,并將流元素分為滿足謂詞的元素和不滿足謂詞的元素。 在此過程中,還演示了便捷方法,例如Pattern.asPredicate()和Predicate.not() 。
翻譯自: https://www.javacodegeeks.com/2018/07/deferred-execution-javas-predicate.html
java延時執行
總結
以上是生活随笔為你收集整理的java延时执行_Java谓词的延迟执行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么进网站源码的后台(网站源码怎么运行)
- 下一篇: java美元兑换,(Java实现) 美元