天天看點

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