java8 Stream類常用方法總結

Java8中提供了Stream對集合操作作出了極大的簡化,學習了Stream之后,我們以后不用使用for循環就能對集合作出很好的操作。

一、流的初始化與轉換:
Java中的Stream的所有操作都是針對流的,所以,使用Stream必須要得到Stream對象:
1、初始化一個流:

    Stream stream = Stream.of("a", "b", "c");

2、數組轉換為一個流:

    String [] strArray = new String[] {"a", "b", "c"};
    stream = Stream.of(strArray);
    或者
    stream = Arrays.stream(strArray);

3、集合對象轉換為一個流(Collections):

    List<String> list = Arrays.asList(strArray);
    stream = list.stream();

二、流的操作:

流的操作可以歸結為幾種:

1、遍歷操作(map):

使用map操作可以遍歷集合中的每個對象,并對其進行操作,map之后,用.collect(Collectors.toList())會得到操作后的集合。

1.1、遍歷轉換為大寫:

List<String> output = wordList.stream().
     map(String::toUpperCase).

           collect(Collectors.toList());

1.2、平方數:

List<Integer> nums = Arrays.asList(1, 2, 3, 4);
   List<Integer> squareNums = nums.stream().
         map(n -> n * n).
       collect(Collectors.toList());

2、過濾操作(filter):

使用filter可以對象Stream中進行過濾,通過測試的元素將會留下來生成一個新的Stream。

2.1、得到其中不為空的String

List<String> filterLists = new ArrayList<>();
filterLists.add("");
filterLists.add("a");
filterLists.add("b");
List afterFilterLists = filterLists.stream()
       .filter(s -> !s.isEmpty())

        .collect(Collectors.toList());

3、循環操作(forEach):
如果只是想對流中的每個對象進行一些自定義的操作,可以使用forEach:

List<String> forEachLists = new ArrayList<>();
forEachLists.add("a");
forEachLists.add("b");
forEachLists.add("c");

forEachLists.stream().forEach(s-> System.out.println(s));

4、返回特定的結果集合(limit/skip):
limit 返回 Stream 的前面 n 個元素;skip 則是扔掉前 n 個元素:

List<String> forEachLists = new ArrayList<>();
forEachLists.add("a");
forEachLists.add("b");
forEachLists.add("c");
forEachLists.add("d");
forEachLists.add("e");
forEachLists.add("f");
List<String> limitLists = forEachLists.stream().skip(2).limit(3).collect(Collectors.toList());

注意skip與limit是有順序關系的,比如使用skip(2)會跳過集合的前兩個,返回的為c、d、e、f,然后調用limit(3)會返回前3個,所以最后返回的c,d,e

5、排序(sort/min/max/distinct):
sort可以對集合中的所有元素進行排序。max,min可以尋找出流中最大或者最小的元素,而distinct可以尋找出不重復的元素:

5.1、對一個集合進行排序:

List<Integer> sortLists = new ArrayList<>();
sortLists.add(1);
sortLists.add(4);
sortLists.add(6);
sortLists.add(3);
sortLists.add(2);
List<Integer> afterSortLists = sortLists.stream().sorted((In1,In2)->

       In1-In2).collect(Collectors.toList());

5.2、得到其中長度最大的元素:

List<String> maxLists = new ArrayList<>();
maxLists.add("a");
maxLists.add("b");
maxLists.add("c");
maxLists.add("d");
maxLists.add("e");
maxLists.add("f");
maxLists.add("hahaha");
int maxLength = maxLists.stream().mapToInt(s->s.length()).max().getAsInt();

System.out.println("字符串長度最長的長度為"+maxLength);

5.3、對一個集合進行查重:

List<String> distinctList = new ArrayList<>();
distinctList.add("a");
distinctList.add("a");
distinctList.add("c");
distinctList.add("d");
List<String> afterDistinctList = distinctList.stream().distinct().collect(Collectors.toList());

其中的distinct()方法能找出stream中元素equal(),即相同的元素,并將相同的去除,上述返回即為a,c,d。

6、匹配(Match方法):
有的時候,我們只需要判斷集合中是否全部滿足條件,或者判斷集合中是否有滿足條件的元素,這時候就可以使用match方法:
allMatch:Stream 中全部元素符合傳入的 predicate,返回 true
anyMatch:Stream 中只要有一個元素符合傳入的 predicate,返回 true

noneMatch:Stream 中沒有一個元素符合傳入的 predicate,返回 true

6.1、判斷集合中沒有有為‘c’的元素:

List<String> matchList = new ArrayList<>();
matchList.add("a");
matchList.add("a");
matchList.add("c");
matchList.add("d"); 

boolean isExits = matchList.stream().anyMatch(s -> s.equals("c"));

6.2、判斷集合中是否全不為空:

List<String> matchList = new ArrayList<>();
matchList.add("a");
matchList.add("");
matchList.add("a");
matchList.add("c");
matchList.add("d");
boolean isNotEmpty = matchList.stream().noneMatch(s -> s.isEmpty());

則返回的為false

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 你要知道的Java8 匿名內部類、函數式接口、lambda表達式與Stream API都在這里 轉載請注明出處 h...
    WWWWDotPNG閱讀 4,341評論 1 14
  • Streams 原文鏈接: Streams 原文作者: shekhargulati 譯者: leege100 狀態...
    忽來閱讀 5,544評論 3 32
  • Java8 in action 沒有共享的可變數據,將方法和函數即代碼傳遞給其他方法的能力就是我們平常所說的函數式...
    鐵牛很鐵閱讀 1,274評論 1 2
  • 前言: 講Stream之前,先來用個小需求帶入本文。畢竟代碼看的最清楚。 正文: 項目某個頁面有個需求,將關鍵詞和...
    T9的第三個三角閱讀 2,420評論 1 9
  • 這里的Stream并不是指類似于IO中的輸入輸出流之類的一種實際概念,更多的是指一種編程思想,它本身并不執行什么特...
    留給時光吧閱讀 691評論 0 0