日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java 8里的Predicate学习笔记

發布時間:2023/12/19 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java 8里的Predicate学习笔记 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

測試代碼:

MapTest maptest = new MapTest();List<Country> testdata = maptest.getCountry();Map<String, Country> result1 = maptest.getCountryDataMap(testdata);Map<String, Country> result2 = maptest.getCountryDataMap2(testdata);Stream<Country> temp = testdata.stream();Predicate<Country> match = country -> country.getCode().equals("US"); Stream<Country> result = temp.filter(match);System.out.println("Size 2: " + result.count());List<Country> filterResult = testdata.stream().filter((country) -> country.getCode().equals("US")).collect(Collectors.toList());System.out.println("size: " + filterResult.size());filterResult.forEach(System.out::println);System.out.println(maptest.assertEqual2(result1, result2));

Predicate的源代碼:

/*** Represents a predicate (boolean-valued function) of one argument.** <p>This is a <a href="package-summary.html">functional interface</a>* whose functional method is {@link #test(Object)}.** @param <T> the type of the input to the predicate** @since 1.8*/

Predicate接受一個類型作為參數,返回布爾值。

因此代碼里我就可以使用第113行這種箭頭函數,country為箭頭函數或者說Lambda Function里的形式參數(輸入參數),因為Predicate接收的泛型參數為T- Country,所以編譯器能斷定,形式參數的類型一定為Country.

最后用collect,傳入Collectors.toList().

collect方法將傳入的Collector施加到stream的元素內,

Collectors.toList的操作:將流里的元素添加到一個List容器里并返回。

Returns a Collector that accumulates the input elements into a new List. There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned

完整的測試代碼:

package java8.forHybrisCodeReview;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream;class Country{private String mCountryCode;private int mRank;public Country(String mCode, int rank){this.mCountryCode = mCode;this.mRank = rank;}public String getCode(){return this.mCountryCode;}@Overridepublic boolean equals(Object other) {if( other == null)return false;if ( !(other instanceof Country) )return false;Country country = (Country)other;return this.mCountryCode.equals(country.mCountryCode) && this.mRank == country.mRank;}@Overridepublic int hashCode() {return this.mCountryCode.hashCode()*37 + this.mRank;} } public class MapTest{public List<Country> getCountry(){List<Country> countries = new ArrayList<Country>();Country country = new Country("ZH", 1);countries.add(country);country = new Country("US", 2);countries.add(country);country = new Country("JP", 3);countries.add(country);return countries;}public Map<String, Country> getCountryDataMap(List<Country> countryList){final Map<String, Country> countryDataMap = new HashMap<>();for (final Country countryData : countryList){countryDataMap.put(countryData.getCode(), countryData);}return countryDataMap;}public Map<String, Country> getCountryDataMap2(List<Country> countryList){final Map<String, Country> countryDataMap = new HashMap<>();Consumer<Country> consumer = c -> countryDataMap.put(c.getCode(), c);countryList.forEach(consumer);return countryDataMap;}// forEach之所以被稱為內部遍歷器,原因在于一旦它開始執行了,那么遍歷操作就不能夠被輕易中斷。public boolean assertEqual(Map<String, Country> map1, Map<String, Country> map2){if( map1.size() != map2.size())return false;final boolean equal = true;map1.forEach((key, value)->{System.out.println("key of map1:" + key);Country country = map2.get(key);if( !value.equals(country)){}// Void methods cannot return a value// return false;// equal = false; // cannot change final});return equal;}public boolean assertEqual2(Map<String, Country> map1, Map<String, Country> map2){if( map1.size() != map2.size())return false;for (Map.Entry<String,Country> entry : map1.entrySet()) {String key = entry.getKey();Country country = entry.getValue();Country country2 = map2.get(key);if( !country.equals(country2))return false;}return true;}public static void main(String[] arg){MapTest maptest = new MapTest();List<Country> testdata = maptest.getCountry();Map<String, Country> result1 = maptest.getCountryDataMap(testdata);Map<String, Country> result2 = maptest.getCountryDataMap2(testdata);Stream<Country> temp = testdata.stream();Predicate<Country> match = country -> country.getCode().equals("US"); Stream<Country> result = temp.filter(match);System.out.println("Size 2: " + result.count());List<Country> filterResult = testdata.stream().filter((country) -> country.getCode().equals("US")).collect(Collectors.toList());System.out.println("size: " + filterResult.size());filterResult.forEach(System.out::println);System.out.println(maptest.assertEqual2(result1, result2));} }

要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":

總結

以上是生活随笔為你收集整理的Java 8里的Predicate学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。