2019年1月28日 星期一

[Java] Java 8 可使用Stream來判斷元素是否存在陣列中

import java.util.stream.*;
public static void main(String[] args){
        //以4種型態的陣列作範例
        String[] strArr = {"a", "b", "c"};
        Long[] longArr = {1234567890L, 2345678901L, 3456789012L};
        int[] intArr = {1, 2, 3};
        double[] doubleArr = {1.01, 2.02, 3.03};     
      
        //String 陣列使用Arrays.stream進行比對
        boolean strContains = Arrays.stream(strArr).anyMatch("b"::equals);
        System.out.println("Array contains b? "+strContains);
        strContains = Arrays.stream(strArr).anyMatch("d"::equals);
        System.out.println("Array contains d? "+strContains);
      
        //Long 陣列較特別,要先轉成List再使用stream進行比對
        List<Long> longList = Arrays.asList(longArr);
        boolean longContains = longList.stream().anyMatch(n -> n == 1234567890L);

        System.out.println("Array contains 1234567890L? "+longContains);
        longContains = longList.stream().anyMatch(n -> n == 1L);
        System.out.println("Array contains 1L? "+longContains);
      
        //int 陣列使用IntStream進行比對
        boolean intContains = IntStream.of(intArr).anyMatch(x -> x == 3);
        System.out.println("Array contains 3? "+intContains);
        intContains = IntStream.of(intArr).anyMatch(x -> x == 99);
        System.out.println("Array contains 99? "+intContains);
      
        //double 陣列使用DoubleStream進行比對
        boolean doubleContains = DoubleStream.of(doubleArr).anyMatch(x -> x == 2.02);
        System.out.println("Array contains 2.02? "+doubleContains);
        doubleContains = DoubleStream.of(doubleArr).anyMatch(x -> x == 10.99);
        System.out.println("Array contains 10.99? "+doubleContains);
      
        //若用錯stream無法正確判斷,如對int array誤用Stream
        System.out.println("注意:若用錯stream無法正確判斷,如對int array誤用Stream");
        boolean intWrongContains = Arrays.stream(intArr).anyMatch("3"::equals);
        System.out.println("Array contains 3? "+intWrongContains + ""
                + "(3存在intArr中,使用Arrays.stream皆回傳false,"
                + "應使用前面的IntStream寫法才能正確進行判斷)");
        intWrongContains = Arrays.stream(intArr).anyMatch("99"::equals);
        System.out.println("Array contains 99? "+intWrongContains);
    }
 
線上執行請參考(點選左上角的Execute可看執行結果):http://tpcg.io/zODzm6

執行結果:
Array contains b? true
Array contains d? false
Array contains 1234567890L? true
Array contains 1L? false
Array contains 3? true
Array contains 99? false
Array contains 2.02? true
Array contains 10.99? false
注意:若用錯stream無法正確判斷,如對int array誤用Stream
Array contains 3? false(3存在intArr中,使用Arrays.stream皆回傳false,應使用前面的IntStream寫法才能正確進行判斷)
Array contains 99? false
--------------------------------
參考網址:
  1. How do I determine whether an array contains a particular value in Java?(Stack Overflow) 如何判斷指定元素是否存在Java陣列中
  2. Stream anyMatch() in Java with examples(GeeksforGeeks)
  3. LongStream anyMatch() in Java with examples(GeeksforGeeks)
Copyright © 2019 JavaSparrowK All rights reserved.
版權所有‧謝絕轉載

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。