注意:流一旦使用了終止操做流就關閉了,若是要繼續使用一樣的數據源,則需從新獲取流java
filter(Predicate p) : 接收 Lambda , 從流中排除某些元素。
distinct() : 篩選,經過流所生成元素的 hashCode() 和 equals() 去除重複元素,因此要根據實際狀況編寫這兩個方法
limit(long maxSize) : 截斷流,使其元素不超過給定數量。找到知足條件的數據就會終止內循環
skip(long n) : 跳過元素,返回一個扔掉了前 n 個元素的流。若流中元素不足 n 個,則返回一個空流。與 limit(n) 互補
sorted(comparator c): 對流中的元素進行排序dom
List<Cust> custs = Arrays.asList( new Cust(101,"梅西",30,33000000L), new Cust(102,"伊布",35,23000000L), new Cust(103,"哈維",34,20000000L), new Cust(104,"伊列斯塔",33,18000000L), new Cust(105,"小羅",37,15000000L), new Cust(106,"內馬爾",27,32000000L), new Cust(106,"內馬爾",27,32000000L), new Cust(106,"姆巴佩",23,30500000L) ); public void test2() { //找出工資排名前三位的不重複的人,且年齡要小於等於30 Stream<Cust> sm1 = custs.stream(); sm1.filter((x)->x.getAge()<=30) .sorted((x,y)->{ if(x.getSalary()>y.getSalary()) return 1; else if(x.getSalary()<y.getSalary()) return 0; else return -1; }) .distinct() .limit(3) .forEach(System.out::println); System.out.println("######################"); //找出工資排名第二和第三位不重複的人,且年齡要小於等於30 Stream<Cust> sm2 = custs.stream(); sm2.filter((x)->x.getAge()<=30) .sorted((x,y)->{ if(x.getSalary()>y.getSalary()) return 1; else if(x.getSalary()<y.getSalary()) return 0; else return -1; }) .distinct() .skip(1) .limit(2) .forEach(System.out::println); }
真的好強大,之前要寫多少代碼才能夠完成的事情,只須要一句就搞定。函數
filter((x)->x.getAge()<=30)spa
過濾年齡大於30的元素code
sorted((x,y)->{
if(x.getSalary()>y.getSalary())
return 1;
else if(x.getSalary()<y.getSalary())
return 0;
else
return -1;
})對象
對過濾的結果按照工資降序排序排序
distinct()ip
對有序的結果集排重get
limit(3)hash
只取結果集的前三個元素
foreach(System.out::println)
終止操做,使用方法引用(對象::實例方法名)打印出結果集
map(Function f) :接收一個函數做爲參數,該函數會被應用到每一個元素上,並將其映射成一個新的元素。
mapToDouble(ToDoubleFunction f) :接收一個函數做爲參數,該函數會被應用到每一個元素上,產生一個新的 DoubleStream。
mapToInt(ToIntFunction f) : 接收一個函數做爲參數,該函數會被應用到每一個元素上,產生一個新的 IntStream。
mapToLong(ToLongFunction f) :接收一個函數做爲參數,該函數會被應用到每一個元素上,產生一個新的 LongStream。
@Test public void test3() { //map類型:將每一個元素通過處理轉換成另一個類型(能夠是同類型) List<String> names = Arrays.asList("姚明","科比","喬丹","詹姆斯"); names.stream().map(Cust::new).forEach(System.out::println); System.out.println("###################################"); List<String> dbls = Arrays.asList("11","22","33","44"); dbls.stream().mapToDouble(Double::parseDouble).forEach(System.out::println); System.out.println("###################################"); dbls.stream().mapToInt(Integer::parseInt).forEach(System.out::println); System.out.println("###################################"); dbls.stream().mapToLong(Long::parseLong).forEach(System.out::println); System.out.println("###################################"); }
flatMap(Function f) : 接收一個函數做爲參數,將流中的每一個值都換成另外一個流,而後把全部流鏈接成一個流
一樣還有flatMapToInt(),flatMapToLong(),flagMapToDouble(),用法和flatMap一致,只是要求最終處理出來的元素是對應的Int,Long,Double
@Test public void test4() { //flatMap類型:將爲每一個元素建立一個流,並最終合併成一個流 List<List> lp = new ArrayList(); for(int i=0;i<3;i++) { List<Double> lc = new ArrayList(); for(int j=0;j<10;j++) { lc.add(Math.random()); } lp.add(lc); } lp.stream().flatMap((x)->x.stream()).forEach(System.out::println); }